Flixel Forums

development => help => Topic started by: ExpertNovice on Sat, Jun 18, 2011

Title: [SOLVED] what is super.destroy()?
Post by: ExpertNovice on Sat, Jun 18, 2011
I keep looking through Adam's code and I keep seeing super.destroy().  I always put it in my code now but I don't know much about programming so it's functions aren't obvious to my novice brainpowers.
Title: Re: what is super.destroy()?
Post by: cai on Sat, Jun 18, 2011
super is the class which your class inherits/extends.  Let's say you make a Player object, and it extends FlxSprite.  In this case, super inside of Player refers to FlxSprite.

So saying super.destroy() inside Player means you want to call the destroy() function as defined in FlxSprite.  What destroy() does depends on what object it's in--but my guess would be that it does something to destroy it... ;)
Title: Re: what is super.destroy()?
Post by: zadvornykh on Sat, Jun 18, 2011
destroy is the opposite of create. It has to do with garbage collection by the Flash player to clear memory. When you instantiate an object (using new [ClassName]), you create a reference to it and it gets stored in memory. When you no longer need that object, you need to remove all references to it, so the garbage collection can kick in and clear memory allocated to that object.

In Flixel, destroy does just this: removing references to objects created by the Flixel classes.

When you extends a Flixel class, like FlxSprite, and create your own objects, you can (and in many cases should) override the destroy method to clear the references to the objects you created. Calling super.destroy() within that method, makes sure that the references to objects created in FlxSprite get cleared as well.

For example: a Player class which extends FlxSprite
Code: [Select]
private var sword:Sword;

public function Player():void
{
 sword = new Sword(); //create a new instance of the Sword class, and store a reference to it
}

override public function destroy():void
{
 sword = null; //remove reference to sword, so the instance can be cleared by the garbage collection
 super.destroy();
}

For more on garbage collection of the FlashPlayer, read this article: http://www.adobe.com/devnet/flashplayer/articles/garbage_collection.html (http://www.adobe.com/devnet/flashplayer/articles/garbage_collection.html)