在Flash中处于非活动状态一段时间后navigateToURL

时间:2013-06-19 16:20:51

标签: flash

如果鼠标在三分钟内没有移动,我想重新启动应用程序。我使用“navigateToURL”返回第一个场景,但是如何使用actionscript 3实现这样的定时事件?

1 个答案:

答案 0 :(得分:0)

这非常简单 - 你需要使用两个事件监听器:

  1. MouseEvent.MOUSE_MOVE事件侦听器
  2. TimerEvent.TIMER事件侦听器
  3. 您还需要一个Timer对象

    所以这是代码

        //private timer var in your class
        private var myLittleTimer:Timer;
    
        //This is your main function, where you adding all the event listeners
        //IMPORTANT NOTE:
        //We can only track mouse movement over the flash stage in a DisplayObject that
        //has the link to the main stage
        //For example, your main SPRITE
        yourMainFunction():void{
            //Timer initialization
            this.myLittleTimer = new Timer(3000, 1);
            this.myLittleTimer.addEventListener(TimerEvent.TIMER, lazinessDetector);
            this.myLittleTimer.start();
    
            this.addEventListener(Event.ADDED_TO_STAGE, init);
        }
        private function init(e:Event):void{
            this.removeEventListener(Event.ADDED_TO_STAGE, init);
            //THIS IS THE ENTRANCE POINT
            //do not use stage object before the sprite has been added to the stage
            //as it will cause a null object exception
    
            //We add a MOUSE_MOVE event listener to the stage to track the moment when
            //the mouse has moved
            stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
        }
        private function onMouseMove(e:MouseEvent):void{
            this.myLittleTimer.reset();
            this.myLittleTimer.start();
            //This function will reset the "countdown" timer and start it over again.
        }
        private function lazinessDetector(e:TimerEvent):void {
            //YOUR URL REDIRECT CODE GOES HERE
        }