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?
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.