0

After trying to optimize my game i started off with the obvious object pool. I followed the more complicated one using max_growth etc then went on to a simple one but i keep hitting the same problems. Now after over 40hrs of searching online and asking help from a friend who knows a lot more than me about flash i've given up and decided to come here. The code below includes some # just for reference and i've left in my commented stuff which i've tried and also hasn't worked.

My Cpool class

package  {
    import flash.display.DisplayObject;

    public class CPool 
    {
        public var counter:int;
        private var pool:Array;

        //public function CPool(type:class, len:int)
        public function CPool()
        {
            pool = new Array();
            counter = 10;
            //counter=len;

            //var i:int=len;
            var i:int = 10;
            while (--i > -1)
                //pool[i] = new type();
                pool[i] = new Condor();
        };

        public function getCondor():Condor
        {
            trace(counter);
            //trace (pool.length);
            if (counter > 0)
            {
                return pool[--counter];
             }
             else
             {
                 var cde = new Condor();
                 return cde;
              }
        };


        public function disposeCondor(disposedCondor:Condor):void {

            pool[counter++] = disposedCondor;
    //pool.unshift(disposedCondor);     #10
         };
    }
}

You can see it's a simple one that just uses get and dispose. My code on my logic class below that refers to the pool.

public var cpool:CPool;

cpool = new CPool();   //(in the constructor)

else if (whichBird ==12)
{
    bird = cpool.getCondor() as Condor;
}  //  (in the addBird section)


private function checkBirdsOnScreen():void 
{
    for (var i:int = 0; i < birdArray.length; i++)
    {
        var dg = birdArray[i];
        if (birdArray[i].x < -100 )
        {
            dg.parent.removeChild(dg);
            birdArray.splice(i, 1);
            //hc.destroyCondor();                #9
            //tr cpool.disposeCondor(dg);        #1
            //birdArray[i].destroyCondor();     #2
        }
    }
}

and finally my destroy bird function in the condor class

public function destroyCondor():void
{
    //removeChild(oldCondor);  #5           
    //ConPool.disposeCondor(oldCondor); #4
    //this.x=390 #3
//ConPool.disposeCondor(this);
    parent.removeChild(this); //remove object from stage
    removeEventListener(Event.ENTER_FRAME, CondorLoop); 
}   

As you can see i've tried every way i can think of to get this to work. The code as it stands starts off with 10 condors in the pool, when they go off screen they leave and the trace counter in the pool shows the pool going down, when it gets to 0 it creates new condors. Now this works this way or when i use destroyCondor unless i take some comments out. When i remove //tr in the dispose condor then it works for the first 2 condors, the counter goes to 9, then 9 again then 10 and stays at 10, the problem is it stops putting any more on screen, no errors but it stops working. Btw, i've also tried trace (pool.length) but i keep getting 10s all the way down.

I've also tried #1 and 2 with no effect. The closest i've come is #10, the unshift statement in the pool. This actually looks like it works, counter goes down to 9, back up to 10 when they go off screen like it's putting them back into the pool but when 10 have gone past they just stop coming altogether, even bypassing the new condor statement. I really have tried everything that i can think off as you can see from the comments, even going form the more advanced pooling to this simplified version and still getting nowhere. Over 40 hrs now, staying up till 4am as i'm so frustrated at not being able to solve this problem and i haven't even done the bitmaps caches yet. Can anyone please help?

Benoît Guédas
  • 801
  • 7
  • 25

1 Answers1

0

If you need to alter the array while traversing it, do a backwards traverse.

for (var i:int=birdArray.length-1;i>=0;i--) {...}

This eliminates both null pointers trouble, missed elements (if you encounter this as well) and the need of a temporary array.

Vesper
  • 18,599
  • 6
  • 39
  • 61
  • Tried this and still getting the same problem, goes down from 10, 9, 9, 10, 10 so it looks like it's working but it still stops putting them on screen, thanks for the reply though – user1939771 Mar 01 '13 at 12:47
  • It seems that your pool is not made for reusing condors, but you are adding them back into the pool anyway in your `disposeCondor()`, as well as increasing the counter. This makes the next call of `getCondor()` to receive an already disposed condor, which seemingly is against the game logic. You should re-check your logic of how does the pool operate. – Vesper Mar 01 '13 at 12:51
  • I thought that by using the dispose condor i was putting it back in the pool so it could be re-used. I agree that it does seem like it can't call a disposed condor. The logic of the pool though is sound i think. I actually got it from a gotoandlearn video but even so i have used another one which is designed to work on different classes and definitely to be re-used but it still doesn't work with my game. Is there anything in the pool code you can see that would stop it re-using the old condors. – user1939771 Mar 01 '13 at 12:57
  • You have to re-initialize that condor you're stuffing into the pool by some method of Condor class. Add `disposedCondor.reinitialize()` after you add it to the pool, and write the method. – Vesper Mar 01 '13 at 14:08
  • As you probably guessed i'm fairly new to as3, i'm looking on Google now but if you have an easy way to reinitialize then could you post it please. Thanks for all your help on this btw, i think you're right in the problem, i was just thinking now about moving a few things, eg i have the starting position and enterframe for movement in the condor class so i thought moving all that into the main class may work but this sounds easier if it can be done. – user1939771 Mar 01 '13 at 14:15
  • Worked it out and it works great, thanks very much for the help – user1939771 Mar 01 '13 at 14:48