Search code examples
actionscript-3functionflash-cs5

Removing a function and adding it again


I know it's possible to add and remove functions via event listener, but I was curious to know if it's possible to add/remove a function not by event listener. So I guess a custom function

function timer(event:TimerEvent)
{
  example();
}

function example():void
{
  trace("example");
}

Would it be possible to remove the example function then add it again?


Solution

  • As you said, the common way is to use event listeners:

    mytimer.removeEventListener(TimerEvent.TIMER, timer);
    

    If you don't want to remove the listener you could use a flag to toggle example call:

    var tick = false;
    function timer(event:TimerEvent)
    {
        if (tick) {
            example();
        }
    }