开发者

in actionscript 3.0, when removing a child, why does the code still run?

开发者 https://www.devze.com 2023-03-23 15:31 出处:网络
I\'m switching over from Actionscript 2.0 to Actionscript 3.0, and I\'m missing a few key lines of code. Let\'s say I had a missile, and when it leaves the screen, in Actionscript 2, I would just use

I'm switching over from Actionscript 2.0 to Actionscript 3.0, and I'm missing a few key lines of code. Let's say I had a missile, and when it leaves the screen, in Actionscript 2, I would just use

removeMovieClip(this);

In Actionscript 3.0, I think I'm supposed to use

parent.removeChi开发者_高级运维ld(this);

Problem is, the code still runs. For example, I have a trace on frame 1, and then this code on Frame 30. Then when I run the program, it would run frame 1, then the object would "disappear", and then the trace happens again and when Frame 30 comes around again, I get TypeError: Error #1009: Cannot access a property or method of a null object reference.

So what am I doing wrong? Am I not deleting/etc properly? I realize that there are a few questions similar to this but they don't seem to have the answers I'm looking for. Thanks in advance.


Removing a display object from the display doesn't destroy the object. You'll need to destroy the object manually. Here's an illustration of adding and removing display objects:

var mc:MovieClip = new MovieClip();
addChild(mc);
trace(mc); // traces [Object MovieClip]

removeChild(mc);
trace(mc); // traces [Object MovieClip]

mc = null;
trace(mc); // traces null

Keep in mind, setting an object to "null" doesn't necessarily destroy it. If you have event listeners or references to this object in your code, Flash will still hold it in memory. In this case you would want to create a "destroy" function for the object. This function would remove any reference to the object and remove any event listeners.


ActionScript code will continue to execute until the object is cleaned up by the garbage collector. If you want it to stop, you must explicitly stop whatever is causing the code execution. On a movieclip that means calling myMovieClip.stop() if you are relying on an enterframe event, simply remove the listener.


You need to be sure there are no event listeners attached to the movieclip. You also need to be sure that there is no reference (in any array or any other object) to the movieclip. You also need to say mc = null to remove the movieclip itself.

Otherwise FlashPlayer holds the clip in memory and it will exist forever, even when it is already removed from the displaylist.

0

精彩评论

暂无评论...
验证码 换一张
取 消