Author Topic: Mode game: Sounds in Bullet Classes  (Read 2249 times)

isidoro

  • Member
  • **
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Mode game: Sounds in Bullet Classes
« on: Wed, Jul 27, 2011 »
Hi everybody,
I'm looking at Mode game source code, using Flash Builder profiler, and I've seen there is a memory leak with 2 sounds used by Bullet and EnemyBullet classes. These classes uses FlxG.play to play that sounds, so a new sound is created each time the player (or enemy) shoots (although all of them are destroyed when player dies).
But, is there any reason for use FlxG.play, instead of load and store these 2 sounds in 2 FlxSound instances?

Am i missing something?

Thanks.

Wing Eraser

  • Guest
Re: Mode game: Sounds in Bullet Classes
« Reply #1 on: Thu, Jul 28, 2011 »
The sounds are stored in FlxG.sounds (FlxGroup).
When FlxG.play is called, look deeper in the code: var sound:FlxSound = sounds.recycle(FlxSound) as FlxSound;
It recycles the existing sounds.

Have you look the memory usage? It will fall down when the garbage collector kicks in.

isidoro

  • Member
  • **
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Re: Mode game: Sounds in Bullet Classes
« Reply #2 on: Thu, Jul 28, 2011 »
Hi,
I've seen loadSound uses recycle system, but I've made another test with one spaceship shooting 4 Bullets each keystroke and now I'm sure Flash Builder profiler doesn't lie. These are profiler output screenshots:




In Mode, after half a minute of shots there are 83 Bullet_SndShot instances (and they are only destroyed after game restarts). Storing one FlxSound into a Bullet class variable only 17 instances are created (same as Bullet instances).
But in my test I've been shooting for about one minute, and these are profiler results:
- Using sound variable 27 TripleShot sound instances, and 7 SingleShot sound instances were created (Memory usage < 3 Mb).
- But using FlxG.play I have 1164 TripleShot sound instances, and 242 SingleShot sound instances (and Memory usage grows to 15 Mb!!).

osro

  • Active Member
  • ***
  • Posts: 121
  • Karma: +1/-0
    • View Profile
    • Purppura
Re: Mode game: Sounds in Bullet Classes
« Reply #3 on: Thu, Jul 28, 2011 »
I have made this pull request a while ago
https://github.com/osro/flixel/commit/eaa8962b77f79da8799a4e00ea0b1357051faf9e

There is a bug in FlxSound.loadSound and FlxSound.loadEmbedded functions.

FlxG.play function is trying to set AutoDestroy as true, but it's not passed through, and that makes all the sounds you play stay in memory.

Code: [Select]
static public function play(EmbeddedSound:Class,Volume:Number=1.0,Looped:Boolean=false,AutoDestroy:Boolean=true):FlxSound
« Last Edit: Thu, Jul 28, 2011 by osro »

isidoro

  • Member
  • **
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Re: Mode game: Sounds in Bullet Classes
« Reply #4 on: Thu, Jul 28, 2011 »
I have made this pull request a while ago
https://github.com/osro/flixel/commit/eaa8962b77f79da8799a4e00ea0b1357051faf9e

There is a bug in FlxSound.loadSound and FlxSound.loadEmbedded functions.

FlxG.play function is trying to set AutoDestroy as true, but it's not passed through, and that makes all the sounds you play stay in memory.

Code: [Select]
static public function play(EmbeddedSound:Class,Volume:Number=1.0,Looped:Boolean=false,AutoDestroy:Boolean=true):FlxSound
Thanks for your clarification!!
So I'll use FlxSound until bug is fixed.

osro

  • Active Member
  • ***
  • Posts: 121
  • Karma: +1/-0
    • View Profile
    • Purppura
Re: Mode game: Sounds in Bullet Classes
« Reply #5 on: Fri, Jul 29, 2011 »
Thanks for your clarification!!
So I'll use FlxSound until bug is fixed.

The bug is in the FlxSound ;D Look the link I posted.
It's doesn't pass the parameter to the private variable.

isidoro

  • Member
  • **
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Re: Mode game: Sounds in Bullet Classes
« Reply #6 on: Fri, Jul 29, 2011 »
The bug is in the FlxSound ;D Look the link I posted.
It's doesn't pass the parameter to the private variable.
Yes, I want to mean to store sound in a FlxSound instance, and call flxSound.play():
Code: [Select]
private var shootSnd:FlxSound;

//Constructor:
shootSnd = FlxG.loadSound(ShootSnd, 1.0, false, true, false);
//Where AutoDestroy parameter can be true or false ;-)

override public function shoot(position:FlxPoint):void
{
shootSnd.play();
...
}

Thanks!!