How to make a MovieClip remove itself in AS3?

淺唱寂寞╮ 提交于 2019-12-04 03:29:44

问题


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,


回答1:


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.




回答2:


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);
    }
}



回答3:


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;
});



回答4:


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;
}



回答5:


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.



来源:https://stackoverflow.com/questions/3184623/how-to-make-a-movieclip-remove-itself-in-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!