Author Topic: [SOLVED] Performance issue  (Read 1250 times)

akaLander

  • Member
  • **
  • Posts: 32
  • Karma: +0/-0
    • View Profile
[SOLVED] Performance issue
« on: Wed, May 2, 2012 »
Hello. I've got a performance issue which I can't understand. I have a helicopter and it creates new smoke sprite every frame. Here's the part of the code of these two classes:
Code: [Select]
helicopter class update:
...
if (armor <= MaxArmor/3)
{
smoke = new SmokeParticleClass(heliBody.x + 88, heliBody.y + 8);
this.add(smoke);
}
...
Code: [Select]
smoke class:
override public function update():void
{
i += 1
if (i > lifeTime)
{
this.exists = false;
this.kill();
this.destroy();
return;
} else {
... more code
But when the helicopter appears and starts smoking it uses more and more memory and update and drawing time increases. Why is this happening?
« Last Edit: Wed, May 2, 2012 by akaLander »

wg/funstorm

  • Global Moderator
  • Key Contributor
  • *****
  • Posts: 596
  • Karma: +0/-0
    • View Profile
    • Funstorm
Re: Performance issue
« Reply #1 on: Wed, May 2, 2012 »
when the helicopter appears and starts smoking it uses more and more memory and update and drawing time increases. Why is this happening?

I have a helicopter and it creates new smoke sprite every frame.

I think you answered your own question there ;) If you keep creating things, they'll take up more & more memory and performance.

I would recommend you create a new FlxGroup for your smoke sprites, and then instead of doing 'new sprite' each frame you use your group's recycle method. See recycle's comments in the flixel source code for more detailed info on how to use it. If you set it up right, recycle will re-use dead sprites instead of creating new ones when possible.

akaLander

  • Member
  • **
  • Posts: 32
  • Karma: +0/-0
    • View Profile
Re: Performance issue
« Reply #2 on: Wed, May 2, 2012 »
Thanks. I'll try adding all the smoke to one group.
But shouldn't kill(), destroy() and exists = false kill the smoke object, free the memory and make it skip it's own update()?

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Re: Performance issue
« Reply #3 on: Wed, May 2, 2012 »
yes but problem lies when you instantiate new object. That's why funstrom said to use recycle, to overcome this.
blog, twitter, Check out my award winning game, Rot Gut:

pixelomatic

  • Active Member
  • ***
  • Posts: 131
  • Karma: +0/-0
    • View Profile
Re: Performance issue
« Reply #4 on: Wed, May 2, 2012 »
Thanks. I'll try adding all the smoke to one group.
But shouldn't kill(), destroy() and exists = false kill the smoke object, free the memory and make it skip it's own update()?

These variables and functions you are talking about are flixel-defined variables. When you kill an object, it's exists flag will be set to false but it will remain in memory until there are no references to that objects aka garbage collection. Still if you set all references to it refer to null, it will remain in memory until next garbage collection. So it's better to use recycle methods.

akaLander

  • Member
  • **
  • Posts: 32
  • Karma: +0/-0
    • View Profile
Re: Performance issue
« Reply #5 on: Wed, May 2, 2012 »
So it will be recycling. Thanks)