Search code examples
actionscript-3animationdelayed-executionenterframeevent

AS3: Delay Enter Frame Animation


I've got a fade out animation using ENTER_FRAME. I want the fade out to start after 2-3 seconds. How can I create this delay?

txtAlert.addEventListener(Event.ENTER_FRAME,animAlert);

function animAlert(e:Event) {
    if(e.target.alpha>0) {
        e.target.alpha-=0.01;
    } else {
        e.target.parent.removeChild(e.target);
        e.target.removeEventListener(Event.ENTER_FRAME,animAlert);
    }
}

Solution

  • You should use a Timer:

    var timer:Timer = new Timer(3000, 1);
        timer.addEventListener(TimerEvent.TIMER, action);
        timer.start();
    
    function action(evt:TimerEvent):void{
         txtAlert.addEventListener(Event.ENTER_FRAME,animAlert);
    
         trace("Times Fired: " + evt.currentTarget.currentCount);
         trace("Time Delayed: " + evt.currentTarget.delay);
    }
    

    BTW you should look at animation libraries like Twiner that will make your life a lot easier.