actionscript-3flashflash-cs5frameenterframeevent

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


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?


Solution

  • 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