2

So the ENTER_FRAME property will add an object to the stage on every frame the game runs. If the game is 24 fps, 24 objects created per second. How can I limit that so it will generate an object every 4 frames?

GivenPie
  • 1,451
  • 9
  • 36
  • 57

1 Answers1

4

you can have a counter that increments every frame

var f:int = 0;
addEventListener(Event.ENTER_FRAME,onEnterFrame);
function onEnterFrame(e:Event):void{
    if (f%4 == 0){
        // do something
    }
    f++;
}

you can set f=0; inside the if statement if you like

Daniel
  • 34,125
  • 17
  • 102
  • 150
  • Will f=0 mean that there will be no objects that are generated? – GivenPie Apr 04 '12 at 21:42
  • 1
    @GivenPie The only time f%4==0 is when f equal a number that divides by 4 with no remainder IE: 4/8/12/16. Modulus is basic math and you should understand it if you are going to do much coding. – The_asMan Apr 04 '12 at 21:49
  • 3
    Could even remove a line and do `if(0 == ++f % 4)` – Marty Apr 04 '12 at 23:45