How can I specify ENTER_FRAME so that the object enters on every 4th frame?

Go To StackoverFlow.com

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?

2012-04-04 21:23
by GivenPie


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

2012-04-04 21:32
by Daniel
Will f=0 mean that there will be no objects that are generated - GivenPie 2012-04-04 21:42
@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 2012-04-04 21:49
Could even remove a line and do if(0 == ++f % 4)Marty 2012-04-04 23:45
Ads