Skip to main content
Participant
December 28, 2011
Answered

Object Pooling question

  • December 28, 2011
  • 1 reply
  • 1051 views

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);

                    }

This topic has been closed for replies.
Correct answer kglad

Thank you again,  I have 1 last question and then I'll close the thread and mark correct.  I think this is relevant to anyone else that wanted to use this an example for their own applications. 

I implemented your setBall function, worked great.

The last issue I'm having is that my Ball objects have a Timer that's started in the Ball class's constuctor.   This Timer moves them down the screen according to my "gravity" property. When I remove them from the screen and put them back in the pool, I want to stop this Timer to improve performance.  I then want to start the Timer when they come back onto the screen.

It seems to be working except when they are added back to the screen, they appear at the bottom of the screen where they were deleted, INSTEAD of at the top of the screen like they should be. 

Right now, I check the "running" property on the Timer of that particular ball.  Not working though.  What could be causing the Balls to be in the wrong position when re-pulled from the pool?

-----document class-----------

ballArray.updateTimer.stop();

removeChild(ballArray);

Pool.setBall(ballArray);

ballArray.splice(i,1);

return;

------BallPool class---------

public function getBall($position:Point, $vector:Point, $gravity:int, $friction:Number):Ball {

 

                              if(STOREDBALLS.length > 0) {

                                        // grab ball from list + remove it

                                        var ball:Ball = STOREDBALLS.pop();

 

                                        // reset its values

                                        ball.position = $position;

                                        ball.vector = $vector;

                                        ball.gravity = $gravity;

                                        ball.friction = $friction;

 

                                        if(ball.updateTimer.running == false) {

 

                                                  ball.updateTimer.start();

                                        }

 

                            return ball;

                              }

                                        return new Ball($position, $vector, $gravity, $friction);

                    }


in your ball class you should use listeners to add and remove your timer:

this.addEventListener(Event.ADDED_TO_STAGE,addedF);

this.addEventListener(Event.REMOVED_FROM_STAGE,removedF);

function addedF(e:Event):void{

updateTimer.addEventListener(TimerEvent.TIMER,whateverF);

updateTimer.start();

}

function removedF(e:Event):void{

updateTimer.removeEventListener(TimerEvent.TIMER,whateverF);

updateTimer.stop();

}

////////////

i don't see where you're (re)setting your ball x,y properties but from you code, it looks like you should have position setter that assigns those.  do you have such a setter?

1 reply

kglad
Community Expert
Community Expert
December 28, 2011

what's the problem?

Participant
December 29, 2011

Sorry about that, couple problems. 

Well basically I'm just trying to figure out the easiest way to setup a DisplayObject pool using a separate Pool class and the document class together.

First, the getBall function doesn't work right.  When I call "return STOREDBALLS", the Ball Objects don't get added to the screen and I can't figure out way. 

Basically I just need some help structuring my object pool and figuring out how to properly pull from the pool, recycle to the pool, anything you can help with I'll appreciate.

Thank you again

kglad
Community Expert
Community Expert
December 29, 2011

is STOREDBALLS.Used ever false?  it looks undefined from what you showed.  instead of:

   if(STOREDBALLS.Used == false) {

you may want to use

   if(!STOREDBALLS.Used) {

if it's never false, you're returning a new Ball() which isn't in your STOREDBALLS array.  in addition, you're not using any of those parameters to return a STOREDBALLS member which may or may not be a problem.