Author Topic: [SOLVED] what is super.destroy()?  (Read 1328 times)

ExpertNovice

  • Active Member
  • ***
  • Posts: 124
  • Karma: +0/-0
  • Now we're cookin' with bacon!
    • View Profile
[SOLVED] what is super.destroy()?
« 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.
« Last Edit: Tue, Sep 20, 2011 by ExpertNovice »

cai

  • Contributor
  • ****
  • Posts: 465
  • Karma: +0/-0
  • the illest of villains
    • View Profile
    • Brandon Cash
Re: what is super.destroy()?
« Reply #1 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... ;)
Follow me on Twitter | Come join us at #flixel on irc.freenode.net!

zadvornykh

  • Active Member
  • ***
  • Posts: 205
  • Karma: +1/-0
    • View Profile
Re: what is super.destroy()?
« Reply #2 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