0
votes

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.removeChild(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.

3
try this = null after you remove it but should be done from the parent so this.removeChild( child ); child = null;The_asMan
You could also try adding an eventlistener for removeFromStage to do your clean up on that object and halt anything running in it. then of course null it out somehow. This is OOP after all and the object should take care of itself. Aside from the nulling which should be done from the parents view.The_asMan
You might want to check the answer to a similar question I had: stackoverflow.com/questions/6792291/… It's a problem of referencing an object that is not present in the 'root' (aka Frame 1 of any MC).Alex

3 Answers

1
votes

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.

0
votes

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.

0
votes

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.