Home > OS >  AS3: How to ensure a function isn't called more than once per frame?
AS3: How to ensure a function isn't called more than once per frame?

Time:09-17

I have a function that could be called many times, but I don't want it to be executed more than once per frame. What's the best way of implementing that?

CodePudding user response:

Something like a trigger switch you reset once per frame. I believe the script below is self-explanatory enough.

var canTrigger:Boolean;

addEventListener(Event.ENTER_FRAME, resetTrigger);

function resetTrigger(e:Event):void
{
    canTrigger = true;
}

function oncePerFrame():void
{
    if (!canTrigger) return;
    
    canTrigger = false;
    
    // Your code from here on.
}
  • Related