Copy link to clipboard
Copied
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 ?
When you return boxes to the pool, are you resetting their values?
Copy link to clipboard
Copied
show the code what exactly the pool_Box.getSprite() does
Copy link to clipboard
Copied
sorry for the late Reply
the Pool_Box.getSprite() does this
package {
import flash.display.DisplayObject;
public class SpritePool {
private var pool:Array;
private var counter:int;
private var classRef:Class;
public function SpritePool(type:Class, len:int) {
pool = new Array();
counter = len;
classRef = type;
var i:int = len;
while (--i > -1) {
pool = new type();
}
}
public function getSprite():DisplayObject {
if (counter > 0) {
return pool[--counter];
} else {
increasePool(10);
return getSprite();
}
}
public function increasePool(amount:int):void {
counter += amount;
while( --amount > -1 )
pool.unshift ( new classRef() );
}
public function returnSprite(s:DisplayObject):void {
pool[counter++] = s;
}
}
Copy link to clipboard
Copied
"passedCenter" is a property you assign dynamically.
This can`t be done with any DisplayObject.
I assume your Box class extends MovieClip?
Try to change every instance of DisplayObject in your SpritePool class to MovieClip and see what that does.
Copy link to clipboard
Copied
sorry for the late response, but had to attend classes and other Collewge staff.
I did what you told me (replaced DisplayObject with MovieClip) but nothing happens.
After the 1st item that I create in the main() function is off the stage , the rest dont seem to respond of the change of myBox.passedCenter.
I was thinking of using of getter and setter methods but got to the conclusion that I am doing nearly the same.
Any other ideas ?
Copy link to clipboard
Copied
When you set up your pool, what arguments did you use? (Did you ask it to make instances of Box?)
Copy link to clipboard
Copied
Yes.
I did this private var pool_Box:SpritePool;
and in the main Function pool_Box = new SpritePool(Box, 20)
Copy link to clipboard
Copied
OK, so it seems like it actually should be working. You say you "can't use the properties of Box." Are you getting an error, or is your code just not doing what you think it should? What is the code for Box?
Copy link to clipboard
Copied
well it doesnt show me any errors. its just not doing what it should.
the code is this
package {
import flash.display.MovieClip;
public class Box extends MovieClip {
public var passedCenter:Boolean = false;
public var columnMaker:Boolean = false;
public function Box() {
// constructor code
}
}
}
Copy link to clipboard
Copied
You don't have logic that does anything when passedCenter changed, so what were you hoping to see?
Copy link to clipboard
Copied
to create a box i need 2 things
if(myBox.x <= stage.stageWidth && !myBox.passedCenter)
so when those 2 conditions are made I create a new item that ist X = stage.stageWidth + newBox.Width
that means that there will be a a little time between creating a new Box ( cause myBox.x -=3;).
and by defaut !myBox.passedCenter = false, so thats why i change it to true - >> , so after it passes stage.stageWidth it cant create anymore new Box (if there wasnt this condition, there would be constantly new items and it would make Flash crash.)
and after i change the myBox.passedCenter = true; i create a new item and set its X again to X = stage.stageWidth + newBox.Width and its 1st state of
!myBox.passedCenter is again false so it can create a new item after time.
this thing works perfectly when its a myNewBox:Box = new Box().
in short what am I doing -> moving objects from the stage.stageWidth + box.Width to the left side of the stage
Copy link to clipboard
Copied
When you return boxes to the pool, are you resetting their values?
Copy link to clipboard
Copied
hmmmm if you are asking if when ther returnt to the spritepool
with this line
public function returnSprite(s:DisplayObject):void {
pool[counter++] = s;
}
... well im not.And this might be whhy when they come they dont trigger anything.
but how do i return passedCenter:Boolean when its in the pool again ? or somewhere else where i dont make a mess
Copy link to clipboard
Copied
well after a good night sleep i came with a solution ( dont know if its very good , but it does the job)
I only added one sentance before i remove and splice the object
if(myBox.x < -50 )
{
myBox.passedCenter = false
myArray.splice(i, 1);
removeChild(myBox);
pool_Box.returnSprite(myBox);
}
Thanks for the help.
Copy link to clipboard
Copied
Glad you worked it out
Find more inspiration, events, and resources on the new Adobe Community
Explore Now