Skip navigation
maxwelladdicted
Currently Being Moderated

Bin icon deletes an Movieclip

May 6, 2012 3:24 AM

Tags: #as3 #hittest #action_script_3 #ac3 #bin

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 ***** of code for the above

 

bin.addEventListener(MouseEvent.CLICK, hit);

function hit(event:MouseEvent):void{

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

                    bin.myText.text = "ok";

          }

}

 
Replies
  • Currently Being Moderated
    May 6, 2012 4:42 AM   in reply to maxwelladdicted

    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

     
    |
    Mark as:
  • Currently Being Moderated
    May 6, 2012 5:12 AM   in reply to maxwelladdicted

    It won't work the way you have it because it is not repeatedly looking for the hitTest.  It only checks that one time which is likely before you get a chance to even move the object.  To do the hitTest such that it registers as soon as a hit occurs, you need to be constantly checking.  Doing the hitTest in the stopDrag end of things could work that way but not at the starting end before the object gets moved.

     
    |
    Mark as:
  • Currently Being Moderated
    May 6, 2012 5:22 AM   in reply to maxwelladdicted

    "while" loops are notorious for creating infinite loops and are better avoided for that reason unless necessary - though I don't see where it would readily lead to that the way you are using it.  Once you drop the object, a single if() check should take care of things.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points