每20秒调用一次Flash AS3功能

时间:2011-11-28 05:53:59

标签: flash actionscript-3 function increment

如何在AS3 Flash中设置要递增的功能。我正在尝试在视频启动时启动递增功能,然后每隔20秒运行相同的功能,直到视频停止。

类似的东西:

    my_player.addEventListener(VideoEvent.COMPLETE, completePlay);
    my_player.addEventListener(VideoEvent.PLAYING_STATE_ENTERED, startPlay);

   function startPlay(){
       startInc();
       //OTHER items are started and set within this function that do not have to do with the incremented function.
    }


   function completePlay(){
       //This is where the startInc is stopped but not removed since it will be used again.

    }


     function startInc(){
          //This function should run every 20 seconds.
     }

2 个答案:

答案 0 :(得分:5)

在玩家的VideoEvents周围使用计时器。

package
{
    import flash.display.Sprite;
    import flash.events.TimerEvent;
    import flash.events.VideoEvent;
    import flash.utils.Timer;

    public class IncrementTimer extends Sprite
    {

        private var my_player:*;

        private var timer:Timer;

        public function IncrementTimer()
        {
            my_player.addEventListener(VideoEvent.COMPLETE, completePlay);
            my_player.addEventListener(VideoEvent.PLAYING_STATE_ENTERED, startPlay);
        }

        protected function startPlay(event:VideoEvent)
        {
            timer = new Timer(20000);
            timer.addEventListener(TimerEvent.TIMER, startInc);
            timer.start();
        }

        protected function completePlay(event:VideoEvent)
        {
            timer.reset();
        }

        protected function startInc(event:TimerEvent)
        {
            // called every 20-seconds
        }

    }
}

答案 1 :(得分:1)

var i:uint;

function startPlay(){
    i=setInterval(startInc, 20000);
}


function completePlay(){
    clearInterval(i);
}


function startInc(){
     //This function will run every 20 seconds.
}