destroy (A.K.A. gabage collect) a movieclip/textfield

First of all, in order to free memory, any reference to the instance
needs to be deleted.

Secondly, removeMovieClip() will remove a MovieClip instance from Stage and removeTextField() will remove a TextField instance from Stage.

var tn:MovieClip = this.createEmptyMovieClip(“tn”, 0);
var tn_nested:MovieClip = tn.createEmptyMovieClip(“tnNested”, 0);
var tf_nested:TextField = tn.createTextField(“tf”, 1, 0, 100, 100, 100);
tf_nested.text = “hello world!”;
tn.loadMovie(“http://assets0.twitter.com/images/twitter.png?1175908827“);

destroy_btn.onRelease = function(){
tf_nested.removeTextField();

tn.removeMovieClip();

trace(“is tn instanceof MovieClip after removeMovieClip: “+(tn
instanceof MovieClip));//false
trace(“is tn instanceof Object after removeMovieClip: “+(tn
instanceof Object));//false
trace(“tn type after removeMovieClip: “+ typeof(tn));//movieclip
trace(“is tf_nested instanceof Object after removeMovieClip:
“+(tf_nested instanceof Object));//false
trace(“tf_nested type after removeMovieClip: “+(typeof(tf_nested)));/

/movieclip
trace(“tn is “+tn+”; tf_nested is “+tf_nested);//tn is ; tf_nested is

delete tf_nested;
delete tn;

trace(newline+”>>>>delete tn>>>>”);
trace(“tn after destroy: “+(tn instanceof MovieClip));
trace(“tn after destroy: “+(tn instanceof Object));
trace(“tn type after destroy: “+ typeof(tn));
trace(“tf_nested type after destroy: “+(typeof(tf_nested)));
}

 

The code above first calls removeMovieClip()/removeTextField() first to remove the instances in question and trace down and delete all their references to completely destruct them for garbage collection.

 

BTW,  instanceof MovieClip will return false after “removeMovieClip” is invoked on an mc while typeof(mc) will still return “movieclip”.

Advertisements

One Response to “destroy (A.K.A. gabage collect) a movieclip/textfield”

  1. kevin Says:

    Thanks, could not figure out how to delete dynamically created TextField instances without this.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: