Skip to main content
Known Participant
December 10, 2013
Answered

how to give an ObjectPooled element properties of an class

  • December 10, 2013
  • 1 reply
  • 2001 views

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 ?

This topic has been closed for replies.
Correct answer Amy Blankenship

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


When you return boxes to the pool, are you resetting their values?

1 reply

Inspiring
December 10, 2013

show the code what exactly the pool_Box.getSprite() does

Known Participant
December 10, 2013

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;

                    }

          }

Inspiring
December 11, 2013

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