How to detect number of quickobjects created
How to detect the number of quickobjects created? quickobject are object made from quickbox2d.
Basically, I have a timer that will countdown 4 seconds and it will create a ball dropping down, it will get removed when it falls to the bottom. However I want it in such a way that if more than 5 balls are in the stage, it will stop creating until it is less than 5 to start creating balls again. Is there any way I can do that?
This is the timer to create the ball
if (!_ballCreation_timer) {
_ballCreation_timer = new Timer (4000);
_ballCreation_timer.addEventListener(TimerEvent.TIMER, _onBallCreationTimer);
}
_ballCreation_timer.start();
Function that create ball based on timer
function _onBallCreationTimer (e:TimerEvent):void{
_doCreateNewBall();
}
function _doCreateNewBall(){
_ball_quickobject = sim.addCircle({x:PHYSICS_SCALE * (960), y:PHYSICS_SCALE * (10), radius:0.5, draggable:false});
// KEEP A LIST OF BALLS
_balls_array.push (_ball_quickobject);
}
This is the part where if the ball (QuickObject) from the array fall to bottom and get destroyed
addEventListener(Event.ENTER_FRAME, _onEnterFrame);
//TRACE EACH BALL FALLING OFFSCREEN
function _onEnterFrame (aEvent : Event):void{
for each (var quickObject : QuickObject in _balls_array) {
// BALL FALL OFFSCREEN?
if (quickObject.y / PHYSICS_SCALE > 1080) {
_doHandleBallOffScreen(quickObject);
}
}
}
//DESTROY BALL IF FALL OFFSCREEN
function _doHandleBallOffScreen (aQuickObject : QuickObject):void{
// DESTROY OBJECT
aQuickObject.destroy();
// REMOVE FROM CUSTOM ARRAY
_balls_array.splice (_balls_array.indexOf(aQuickObject),1);
}