Author Topic: How to actually destroy an object in flixel?  (Read 4003 times)

hima

  • Member
  • **
  • Posts: 96
  • Karma: +0/-0
    • View Profile
    • My Dev Blog
I was wondering how should I really destroy an object of a FlxCore or FlxSprite from the game? There are many things such as kill, destroy, active and exists, which seems to do the similar stuffs. But which one should I really choose?

- EDITED -
I'm also wondering how to show a console screen using flixel 1.4? I know that in previous version we can call it by pressing 1, but what about the new version?

Thank you in advance!
« Last Edit: Tue, Jan 5, 2010 by hima »

joybite

  • Guest
Re: How to actually destroy an object in flixel?
« Reply #1 on: Tue, Jan 5, 2010 »
If you don't want to use an object, call kill() on it. If you look inside FlxCore.as, you'll see that inside the function, all it does is set "exists" to false and "dead" to true. The destroy() function doesn't do anything by itself, but (correct me if I'm wrong) is meant to be overrided to do some custom stuff. It is only called when the state changes. You should know that you can't actually "destroy" an object (a limitation of as3). By calling kill(), all you're doing is telling flixel to ignore the object for now. The object will still update though, I believe. If you don't want the object to update anymore, set "active" to false.

On a side note, I think adam advocates reusing objects instead of instantiating new ones in flixel for speed. You would do this by calling reset() on a dead object.

Bring up the console screen by pressing the tilde key (~).

fefranca

  • Guest
Re: How to actually destroy an object in flixel?
« Reply #2 on: Tue, Jan 5, 2010 »
If you look inside FlxCore.as, you'll see that inside the function, all it does is set "exists" to false and "dead" to true. The destroy() function doesn't do anything by itself, but (correct me if I'm wrong) is meant to be overrided to do some custom stuff.

Actually, destroy() doesn't do anything in FlxCore. More complex objects override this function to reset their internal variables, for example FlxLayer empties its children array on destroy().

AVM2 uses automatic garbage collection, nevertheless you do have to worry about memory management, making sure the garbage collector will see the object as garbage (yay I should be a teacher :P). To do that, remove references to the object you want to see destroyed. For example set a variable to null, empty an array (myArray.length = 0 works just fine), etc. The destroy() function does that sort of thing to Flixel's internals.

Like joybite said, try to reuse your objects as that will greatly improve the performance of your game.

joybite

  • Guest
Re: How to actually destroy an object in flixel?
« Reply #3 on: Tue, Jan 5, 2010 »
I should clarify, I meant that kill(), not destroy(), sets "exists" to false and "dead" to true.

fefranca

  • Guest
Re: How to actually destroy an object in flixel?
« Reply #4 on: Tue, Jan 5, 2010 »
I should clarify, I meant that kill(), not destroy(), sets "exists" to false and "dead" to true.

Meh, my mistake sowwy :P

vonWolfehaus

  • Active Member
  • ***
  • Posts: 247
  • Karma: +0/-0
    • View Profile
    • Cold Constructs
Re: How to actually destroy an object in flixel?
« Reply #5 on: Tue, Jan 5, 2010 »
WITH FIRE
Meet Obama every day at #flixel on irc.freenode.net.
Use your favorite IRC client or  http://webchat.freenode.net/

eddietree

  • Member
  • **
  • Posts: 64
  • Karma: +0/-0
  • Bees nuts
    • View Profile
    • Illogictree
Re: How to actually destroy an object in flixel?
« Reply #6 on: Thu, Jan 14, 2010 »
AVM2 uses automatic garbage collection, nevertheless you do have to worry about memory management, making sure the garbage collector will see the object as garbage (yay I should be a teacher :P). To do that, remove references to the object you want to see destroyed...

I am also having troubles with my app eating up memory when it is played for a long time, so I went to investigate.

And as joybite says, it seems that they kill() function only sets certain variables to be ignored when updating/rendering. I am looking at the flixel code, and it seems that the game still keeps holds a reference to that object in the state's layer and thus is still held in memory. The only time I really see the object's reference gets deleted is when the state's layer is deleted, which occurs when you exit the gameState. Thus, the engine seems to keep all of your sprites and stuff until you switch gamestates...

My question is: Is there any way to garbage collect "dead" sprites/objects from the current state?
Dev Blog: illogictree | Twitter: @eddietree

fefranca

  • Guest
Re: How to actually destroy an object in flixel?
« Reply #7 on: Fri, Jan 15, 2010 »
Hi eddie, you are completely right. FlxLayer lacks a "removeChild" method thus it keeps a reference to all sprites within. Just a few tips that can help you fixing that:

- Try to reuse your objects as much as possible, that same enemy doesn't need to be a different one - think about the Rambo series (I saw the same guy die like 5 times in Rambo II :D).

- Extend FlxLayer to include a method to remove children.

- Use FlxLayer.children() method to remove the children yourself (didn't test this, just an idea).

- Use Flex Builder's memory profiler to have a look at the objects in your memory (this is great for identifying memory leaks in any case!)

hima

  • Member
  • **
  • Posts: 96
  • Karma: +0/-0
    • View Profile
    • My Dev Blog
Re: How to actually destroy an object in flixel?
« Reply #8 on: Sun, Jan 17, 2010 »
Thank you everyone! I'm now reusing everything using a very classic way with array set/unset flag. It works pretty well, though I'm sure it's because the game is small. With bigger game, I'd have to look for a better and more scalable way. I'm thinking about some sort of resource pool design pattern. May have to look into that.

Also, thanks for the info, eddietree! I didn't know about that before. That means the garbage collection would never kick those garbage out unless we change the state then.

fefranca, I've tried removing them directly from FlxLayer but it gets really messy since FlxLayer.childern() returns a normal array, which , unlike ArrayList, doesn't have a function that allow you to remove an item at a specific position.

fefranca

  • Guest
Re: How to actually destroy an object in flixel?
« Reply #9 on: Sun, Jan 17, 2010 »
Hi hima, not really.. you could use splice(). Try this:
Code: [Select]
//removing enemy from state's main layer
var pos:Number = _layer.children().indexOf(enemy);
_layer.children().splice(pos, 1);

hima

  • Member
  • **
  • Posts: 96
  • Karma: +0/-0
    • View Profile
    • My Dev Blog
Re: How to actually destroy an object in flixel?
« Reply #10 on: Sun, Jan 17, 2010 »
Yeah that's what I did. But somehow it gives me error and doesn't really do what I want :S Maybe it's the reusing the enemy part that broke it. I'll give it a try again next game and see if it work out for me.