I'm having trouble setting up my object pool. I created a "BallPoll" custom class to handle the pooling logic. I first call fillPool() to add 20 Ball objects into my array. Then in my document class when I want to create a Ball, I check the pool array. It isn't working and I'm not sure why.
------Document Class---------
function throwBall(e:TimerEvent):void {
if (mouseY>stage.stageHeight-180) {
return;
}
var tBall:Ball = Pool.getBall(new Point(mouseX,mouseY),new Point(Math.random()+Math.random()*5+Math.random()*8),gravity,friction);
tBall.gotoAndStop(BallColor);
addChild(tBall);
ballArray.push(tBall);
}
-----------BallPool class---------
package {
import flash.events.TimerEvent;
import flash.geom.Point;
import flash.events.*;
import flash.display.*;
import flash.utils.*;
import flash.system.*;
import Ball;
public class BallPool extends MovieClip {
private static const gravity:Number=1.5;
private static const friction:Number=.50;
public var STOREDBALLS:Array = new Array();
public function BallPool () {
fillPool();
}
public function fillPool() {
for (var i:int = 0; i < 20; i++) {
var NewBall:Ball = new Ball(new Point(mouseX,mouseY),new Point(Math.random()+Math.random()*5+Math.random()*8),gravity,friction);
STOREDBALLS.push(NewBall);
}
}
public function getBall($position:Point, $vector:Point, $gravity:int, $friction:Number):Ball {
if (STOREDBALLS.length > 0) {
var counter:int = 0;
trace(STOREDBALLS.length);
var ball:Ball = STOREDBALLS[counter];
trace("44");
counter ++;
return ball;
} else {
return new Ball($position, $vector, $gravity, $friction);
}
//return null;
}
}
}