how to give an ObjectPooled element properties of an class
Currently I`m trying to create an infinite effect of moving Objects.The problem comes when i change the var myNewBox:Box = new Box(); with
var myNewBox:Box = pool_Box.getSprite() as Box;. It seems that I cant use the properties in the Box() Class and with that my code doesnt do what it was ment to do.
(getSprite() is the object pooling class)
To be more specific Box() is a simple MovieClip of a black square with a only prorertie public var passedCenter:Boolean = false; and depending ot that value i create an object
So the Main Class looks like this:
when i try to use the Objectpooling.
public function Main()
{
var item:Box = new Box();
item.x = stage.stageWidth + item.width;
item.y = 40;
addChild(item);
myArray.push(item);
myRandomEffectNumber = getNextRandomNum();// set the starting effect
stage.addEventListener(Event.ENTER_FRAME, everyFrame);
}
private function everyFrame(ev:Event):void
{
for(var i:int = 0; i< myArray.length; i++)
{
myBox = myArray;
myBox.x -=3;
if(myBox.x <= stage.stageWidth && !myBox.passedCenter)
{
myBox.passedCenter = true;
var myNewBox:Box = pool_Box.getSprite() as Box;
myNewBox.x = stage.stageWidth + myNewBox.width + myNewBox.width * 0.5;
switch (myRandomEffectNumber)
{
case 1 :
myNewBox.y = effect_1();
break;
case 2 :
myNewBox.x = stage.stageWidth + myNewBox.width + myNewBox.width * 0.25; // we cahnge the was number so we create faster the elements
myNewBox.y = effect_2();
break;
case 3 :
myNewBox.y = effect_3();
break;
default :
trace("SWITCH ERROR");
break;
}
addChild(myNewBox);
myArray.push(myNewBox);
}
if(myBox.x < -50 )
{
bulletsArray.splice(i, 1);
removeChild(b);
pool_Box.returnSprite(b);
}
}
so what happends is that !myBox.passedCenter doesnt ever get assigned to myNewBox:Box = pool_Box.getSprite() as Box or atleast it acts like it.
but when a replace myNewBox:Box = pool_Box.getSprite() as Box with siple var myNewBox:Box = new Box();
if(myBox.x < -50 )
{
removeChild(myBox);
myArray.splice(i, 1);
}
it works fine
so can someone help ?
