0

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

4

1 回答 1

0

这很简单——你需要使用两个事件监听器:

  1. MouseEvent.MOUSE_MOVE 事件监听器
  2. TimerEvent.TIMER 事件监听器

您还需要一个 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
    }
于 2013-06-21T06:27:01.330 回答