0

We are a couple of guys making a game. Game entities' data are stored in nested arrays. The top level array contains arrays, which correspond to one entity each. By entity I mean the game objects' parameters stored in the array and the starling object referred to in the array.

These sub-arrays have a reference to a starling movieclip or a starling sprite and around 15 other variables for that entity. The variables are therefore not stored in the movieclip/sprite itself for various reasons. The possibility of Blitting is one of them.

The question is then how to organize this the best way and eventually implement recycling of the starling objects to get less problems with garbage collection. I have three suggestions, but I am aware, that something else could be better.

1) have one 'graveyard' container for each class of object: one for asteroids, one for playershots, one for .... Whenever an entity is destroyed, corresponding object will go to the correct graveyard. Whenever an entity of same class should be created, then reuse one from corresponding container if it holds any.

2) One big container for all destroyed movieclips and sprites. Whenever a sprite or movieclip would spawn, reuse the object from this container if it holds any. Correct Animation should also be set. I don't know if that takes a lot of cpu or is possible at all.

3) Let garbage collection take care of destroyed movieclips and sprites. Don't recycle them.

  • I'm working with flashpunk. So, i've tried to use this recyling stuff to improve game performance. But, the game has become so complex, also i needed a system like yours, i mean, referencing entity stuff to arrays & things like that... But, the recyling stuff has become a real problem, i've tried lots of things to solve that, but every solution was creating another problem! So, i gave up using recyling... – Bora Kasap Feb 16 '14 at 16:50

1 Answers1

0

personally, when i want to do stuff like this i override the base Class i want to use and add some code to do that you want:

For example Asteroid class extending MovieClip :

package
{
    import starling.display.MovieClip;
    import starling.textures.Texture;

    public class Asteroid extends MovieClip
    {
        /** a object pool for the Asteroids **/
        protected static var _pool      :Vector.<Asteroid>;
        /** the number of objects in the pool **/
        protected static var _nbItems   :int;
        /** a static variable containing the textures **/
        protected static var _textures  :Vector.<Texture>;
        /** a static variable containing the textures **/
        protected static var _fps       :int;

        public function Asteroid()
        {
            super( _textures, 60 );
        }

        /** a static function to initialize the asteroids textures and fps and to create the pool **/
        public static function init(textures:Vector.<Texture>, fps:int):void
        {
            _textures   = textures;
            _fps        = fps;

            _pool       = new Vector.<Asteroid>();
            _nbItems    = 0;
        }

        /** the function to call to release the object and return it to the pool **/
        public function release():void
        {
            // make sure it don't have listeners
            this.removeEventListeners();
            // put it in the pool
            _pool[_nbItems++] = this;
        }

        /** a static function to get an asteroid from the pool **/
        public static function get():Asteroid
        {
            var a   :Asteroid;
            if( _nbItems > 0 )
            {
                a = _pool.pop();
                _nbItems--;
            }
            else
            {
                a = new Asteroid();
            }
            return a;
        }

        /** a static function to destroy asteroids and the pool **/
        public static function dispose():void
        {
            for( var i:int = 0; i<_nbItems; ++i )   _pool[i].removeFromParent(true);
            _pool.length = 0;
            _pool = null;
        }
    }
}

So with that you can have an asteroid with the Asteroid.get(); method, this will get an asteroid in the pool or create one if the pool is empty.

Before you have to init the class with the static function Asteroid.init(textures, fps)

When you don't need asteroid anymore you can call myAsteroid.release(); this will return the asteroid to the pool for a next use.

When you don't need asteroids anymore, you can call the static Asteroid.dispose(); to clear the pool.

Hope this could help you.

Benjamin BOUFFIER
  • 946
  • 1
  • 5
  • 7