flash-cs4actionscript-3movieclip

How to make a MovieClip remove itself in AS3?


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,


Solution

  • 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.