Skip to main content
Inspiring
May 6, 2012
Answered

Bin icon deletes an Movieclip

  • May 6, 2012
  • 1 reply
  • 1022 views

Hi all

I got sort o stuck with a simple interaction I'm trying to do... Hope somebody can help me out

I have a button that everytime it is clicked, creates an instance of a MovieClip, puts it in an array and display it on screen

menu_event.addEventListener(MouseEvent.CLICK, clickHandler);

function newMcEvent():void

{

          var clip:MovieClip = new mc_event(); //mc_event is the name of the Mc in the library

          addChild(clip);

          clip.x = 50;

          clip.y = 350;

eventArray.push(clip);

          trace(eventArray+clip.name);//this will show the array list plus the name of the last in, just for feedback

}

Keep in mind that the mc_event movieClip is draggable...

On stage I also have movieClip called bin which should delete whatever clip i'm dragging on it. The simplest I guess would be to have a function that says:

If mouse.target hits the bin, then removeChild(mouse.target)

Does it make sense?

here is may failed chink of code for the above

bin.addEventListener(MouseEvent.CLICK, hit);

function hit(event:MouseEvent):void{

          if(event.currentTarget.hitTestObject(bin)){

                    bin.myText.text = "ok";

          }

}

This topic has been closed for replies.
Correct answer maxwelladdicted

actually...

A suggestion about how to remove the touching movieCLip is more than welcome... After three days of coding I'm starting to see variables eveywhere!


here it is.... Works liek a charm

function touch(e:MouseEvent):void

{

          if (e.target.hitTestObject(this.bin))

          {

                    //trace("FUOCO!!!!");

                    var len:int = this.numChildren;

                    for (var i:int = 0; i < len; i++)

                    {

                              var display:DisplayObject = this.getChildAt(i);

                              trace("display "+ display.name);

                    }

                    var g:String = e.target.name;//get the name of e.target to g

                    var nameOf:DisplayObject = this.getChildByName(g);//finds g inside list

                    trace("eve is in position "+this.getChildIndex(nameOf));

                    //trace("g is "+ g);

                    var indx:int = this.getChildIndex(nameOf);

                    //trace(indx);

                    removeChildAt(indx);

          }

          else

          {

                    trace("acqua");

          }

}

1 reply

Ned Murphy
Legend
May 6, 2012

When you assign the listener to the bin, that will end up being the currentTarget of the event, not the object you are dragging

A CLICK event involves two events occuring... a mousedown followed by a mouse up. I don't think you are clicking on the bin under these circumstances.

If the idea is to have the dragged object go away when it hits the bin, then you should assign a listener that constantly checks to see if that's the case, such as a MOUSE_MOVE listener along with your hitTest code.  Assign the listener to the object your are dragging.

If you want to drop the object on the bin and then have it disappear, then use a dropTarget test instead of a hit test

Inspiring
May 6, 2012

@Ned: I just read you message I'll try the dropTarget test in a second...

In the meantime I thought this might work as well... every new event_obj I create in the main timeline, will also automatically create an area on screen called bin (which is in fact another Movieclip)...

Then I create a if statement inside event_obj that says

if event_obj (the mc I am dragging around) hits the bin movieClip, then remove event_obj...

something like this

  // Register mouse event functions

this.addEventListener(MouseEvent.MOUSE_DOWN, drag);

this.addEventListener(MouseEvent.MOUSE_UP, noDrag);

var bin:MovieClip = new mc_bin();

          addChild(bin);

          bin.x = 350;

          bin.y = 350;

// Define a mouse down handler (user is dragging)

function drag(event:MouseEvent):void {

          var dragged = event.target;

  dragged.startDrag();

          if(dragged.hitTestObject(bin)){

                    trace("ok");

          }

}

does that make sense?

Inspiring
May 6, 2012

addition...

// Register mouse event functions

this.addEventListener(MouseEvent.MOUSE_DOWN, drag);

this.addEventListener(MouseEvent.MOUSE_UP, noDrag);

var bin:MovieClip = new mc_bin();

addChild(bin);

bin.x = 350;

bin.y = 350;

// Define a mouse down handler (user is dragging)

function drag(event:MouseEvent):void

{

          var dragged = event.target;

          // we should limit dragging to the area inside the canvas

          dragged.startDrag();

}

function noDrag(event:MouseEvent):void

{

          var notDragging = event.target;

          notDragging.stopDrag();

          while (notDragging.hitTestObject(bin))

          {

                    trace(notDragging.x);

                    removeChild(notDragging);

                    removeChild(bin);

          }

}

The above code works...

@ned: still trying your suggestion!

kudos again