Skip to main content
Participating Frequently
March 15, 2013
Answered

How to detect number of quickobjects created

  • March 15, 2013
  • 1 reply
  • 643 views

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

                    }

This topic has been closed for replies.
Correct answer kglad

check the length of _balls_array.  if it's greater than 5, don't create another ball:

if (!_ballCreation_timer) {

                                        _ballCreation_timer = new Timer (4000);

                                        _ballCreation_timer.addEventListener(TimerEvent.T IMER, _onBallCreationTimer);

                              }

                              _ballCreation_timer.start();

Function that create ball based on timer

function _onBallCreationTimer (e:TimerEvent):void{

                              _doCreateNewBall();

}

function _doCreateNewBall(){

if(_balls_array.length<=5){

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

                    }

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
March 15, 2013

check the length of _balls_array.  if it's greater than 5, don't create another ball:

if (!_ballCreation_timer) {

                                        _ballCreation_timer = new Timer (4000);

                                        _ballCreation_timer.addEventListener(TimerEvent.T IMER, _onBallCreationTimer);

                              }

                              _ballCreation_timer.start();

Function that create ball based on timer

function _onBallCreationTimer (e:TimerEvent):void{

                              _doCreateNewBall();

}

function _doCreateNewBall(){

if(_balls_array.length<=5){

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

                    }

NewbielarAuthor
Participating Frequently
March 16, 2013

oh yea thanks haha i just realised i forgot about the array.length method !

kglad
Community Expert
Community Expert
March 16, 2013

you're welcome.