How to make a MovieClip remove itself in AS3?
Asked Answered
D

5

6

What is the equivalent to removeMovieClip() in AS3?

Apparently many have the same question:
StackOverflow:

  1. How to completely remove a movieclip in as3
  2. Remove movie clip as3
  3. How to remove childmovieclip and add to new parent movieclip

Others:

  1. removeMovieClip(this) in AS3?
  2. Destroy/Delete a Movieclip???
  3. Remove movie clip

But none of their solutions seem to work, for me:

Im working on flash CS4 with AS3:

I have a very simple movie with a single button called click. On pressing the button, a new instance of coin is created:

this.click.addEventListener(MouseEvent.CLICK,justclick);
function justclick(e:MouseEvent){
    var money=new coin
    this.addChild(money)
    money.x=e.stageX
    money.y=e.stageY
}

It might not be the best code, but it works fine. Now, the coin MovieClip is supposed to show a small animation and remove itself. In good old AS2 I would have added:

this.removeMovieClip()

in the last frame of the animation. But this doesn't exist in AS3.
I have tried, without success:

this.parent.removeChild(this) // 'Cannot access a property or method of nullobject reference'...     

this.removeMovieClip() // 'removeMovieClip is not a function'      

removeMovieClip(this) //'call to possibly undefined method removeMovieClip'       

unloadMovie(this)//'call to possibly undefined method removeMovieClip'       

Solutions?

Thanks,

Dehiscent answered 6/7, 2010 at 8:26 Comment(0)
P
12
this.parent.removeChild(this);

This one should be working; it's what I use. One problem I had when I switched to AS3 is that sometimes it wouldn't be added as a child right, so you might want to check that. You also have to import flash.display via putting this at the top if you're not already:

import flash.display.*

You should also remove the event listener on it before removing it.

Pomona answered 7/7, 2010 at 14:37 Comment(2)
Thanks, it was about the event listeners. Now it works perfectly. BTW what is ´import flash.display.*´ for?Dehiscent
It lets you use functions from the classes inside of the display package that comes with flash. btw, if someone solves your problem you should accept that answer. ;)Pomona
L
0

If your animation is ending on frame 20.

note: using 19 because flash count frames from zero(0) similar to array index.

class animatedCloud
{

    public function animatedCloud(){
        addFrameScript(19, frame20);
    }

    private function frame20(){
        parent.removeChild(this);
    }
}
Lachman answered 22/10, 2012 at 10:18 Comment(0)
L
0

Always ensure that those self removing movieclips can get garbage collected. This solution wiped away all my instances from a loaded swf's library symbol:

var mc:MovieClip = new definition() as MovieClip;
addChild(mc);

mc.x = 1000 * Math.random();
mc.y = 1000 * Math.random();

mc.addFrameScript(mc.totalFrames - 1, function onLastFrame():void
{
    mc.stop();
    mc.parent.removeChild(mc);
    mc = null;
});
Lammastide answered 20/4, 2013 at 12:14 Comment(0)
J
0
public static function removeDisplayObject(displayObject:DisplayObject):void {
    /* normal code
    if(displayObject && displayObject.parent){
        displayObject.parent.removeChild(displayObject);
    }
     */
    displayObject ? displayObject.parent ? displayObject.parent.removeChild(displayObject) : null : null;
}
Janessa answered 5/11, 2013 at 10:27 Comment(0)
F
-1

I use, in an extra blank keyframe at the end of the MovieClip which should remove itself:

stop();
MovieClip(parent).removeChild(this);

Found it to be the proper and best solution.

Fulgurite answered 12/11, 2016 at 12:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.