Object Pooling question
Hey guys, thanks in advance for the great forums.
I'm trying to pool 20 "Ball" objects, hit test them, and removeChild and reuse them. I've got the basic framework setup, but having problems. I have 3 classes involved, Ball class, which you don't need to see. The "BallPool" class that has all pooling logic, and my document class.
In my document class I use a timer to create new "Ball" objects at a set interval. I have to later remove these Ball objects from the screen. I'm trying to do this now with a "Used" boolean property on the Ball object itself.
Any ideas would be greatly appreciated. The simpler the solutions the better, my understanding is still as a beginner.
----- BallPool class code------------
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 {
for (var i:int = 0; i < STOREDBALLS.length; i++) {
if(STOREDBALLS.Used == false) {
STOREDBALLS.Used = true;
return STOREDBALLS;
} /*else {
return new Ball($position, $vector, $gravity, $friction);
}*/
}
return null;
}
}
}
--------Piece from 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);
}
