Cap'nAhab
Cap'nAhab

Reputation: 163

How do you remove a movieclip that is the target of a mouse event?

In my document class, I generate a bunch of movieclips, then position them within their own class file. What I'd like to happen is that when I touch the movieclips with the mouse, they get removed, and the score count has a one point increase. How do you remove a specific movieclip from the canvas, when it triggers a mouse event? Heres what I've tried so far, neither removeChild or removeMovieClip worked:

   public class Pong extends MovieClip
    {
        public var points:int=0;
        public var timer:Timer=new Timer(100);
        public function Pong()
        {
            // constructor code
            timer.addEventListener(TimerEvent.TIMER, tick);
            addEventListener(MouseEvent.MOUSE_OVER,score);
            timer.start();
        }

        public function tick(TimerEvent)
        {
            var clikBloc:clik= new clik();
            addChild(clikBloc);
        }
        public function score(evt:MouseEvent):int
        {
            trace(evt.target);
            points++;
            return (points);
            removeChild(evt.target);
            removeMovieClip(evt.target);
        }

Upvotes: 0

Views: 465

Answers (2)

Urosan
Urosan

Reputation: 321

Your return is to high, so nothing bellow that is executed.

public function score(evt:MouseEvent):int
{
    trace(evt.target);
    points++;

    removeChild(DisplayObject(evt.target));

    // or just to be on safe side try
    // MovieClip(evt.target).parent.removeChild(evt.target)
    return (points);
}

Upvotes: 4

Marty
Marty

Reputation: 39466

You're looking for removeChild().

I normally create a remove() method within the class for the object, so that you can easily call thing.remove();

It would look like this:

public function remove():void
{
    // remove from arrays
    // remove added event listeners

    if(parent) parent.removeChild(this);
}

And better yet, create a base class for your visible elements which has the above remove() method, and then override this in extending classes so that you can remove specific listeners and so on. So assuming that the above is in a base class, this is what the override would look like in an extending class:

override public function remove():void
{
    removeEventListener(MouseEvent.CLICK, _click);
    super.remove();
}

Update as per comment:

You could dispatch an event from the remove() method and listen for it in the document class, which would let you increment the score.

public function remove():void
{
    // dispatch removed event
    var evt:Event = new Event("removed");
    dispatchEvent(evt);

    // remove from arrays
    // remove added event listeners

    if(parent) parent.removeChild(this);
}

Then when you create objects in the document class, use:

thing.addEventListener("removed", removed);

From here you can do (in the document class):

private function removed(e:Event):void
{
    e.target.removeEventListener("removed", removed);

    points ++;
}

Upvotes: 0

Related Questions