0

I'm having trouble setting up my object pool. I created a "BallPoll" custom class to handle the pooling logic. I first call fillPool() to add 20 Ball objects into my array. Then in my document class when I want to create a Ball, I check the pool array. It isn't working and I'm not sure why.

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

}

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

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 {

        if (STOREDBALLS.length > 0) {

            var counter:int = 0;

            trace(STOREDBALLS.length);
            var ball:Ball = STOREDBALLS[counter];
            trace("44");
            counter ++;
            return ball;

        } else { 

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

        //return null;
    }
}
}
shanethehat
  • 15,460
  • 11
  • 57
  • 87
user1114288
  • 13
  • 1
  • 3
  • 1
    Which part isn't working? What exactly is the problem? – Cadin Dec 28 '11 at 23:45
  • The getBall function isn't working. I can't figure out how to loop through STOREDBALLS array and determine which Balls are on the stage and which aren't. Also, properly recycling the the Balls from the parent Document class. Thanks – user1114288 Dec 29 '11 at 04:57

1 Answers1

0

I think a pool should release the ball when giving it back. It is not a list with all you balls (sorry), but it is a list with balls you don't use at the moment. So your getBall() function should return a new Ball and remove the reference from STOREDBALLS. The best way to do this is to use pop() or shift(), which removes the last element from an Array and returns the value of that element.

Your counter is wrong (it is always 0?) and should not used like that.

I would do it like this:

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

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

        return ball;
    } 

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

BTW; It looks like you're coming from a PHP background. In ActionScript 3, nobody uses the dollar signs, you don't need to use them.

Update: To push the ball into the pool again, you could use this function:

public function addBall($ball:Ball):void {
    STOREDBALLS.push($ball);
}

From your class where you are using the pool, you should use removeChild() or addChild(), the handling of the display-list is not the responsibility of the pool-class.

Mark Knol
  • 9,663
  • 3
  • 29
  • 44
  • Thank you for the response! ONE last question please. I've implemented your new getBall function. The Balls unload perfectly, but how do send them back to the array. When I pull them from the pool, I store these balls in an Array called "ballArray" in my document class. "Pool" is the instance of my "BallPool.as" class. I tried calling "Pool.STOREDBALLS.push(ballArray[i]);" but it didn't work. So to summarize how can I send the ball I'm done with back into the STOREDBALLS array which is in the custom class? How do you push objects to Arrays in custom classes FROM the document class? – user1114288 Dec 29 '11 at 17:50