[转]由Timer事件驱动的Flash游戏或者动画的暂停方法

不久之前,我介绍了通过ACTIVATE和DEACTIVATE事件侦测焦点暂停Flash游戏或动画。
在使用Timer事件来驱动Flash游戏或者动画时,这个方法出现了意料之外的问题。
尽管你会认为,我只想需要停止和重新启动timer计时,然后重置内部计时器就可以了。
假如有一个timer事件每秒钟触发一次,如果暂停游戏,然后在小于1秒的时间内容恢复又暂停游戏,那么这个timer事件将无法再次被触发。
解决方法可以这样做,首先记录timer事件上一次触发到Flash暂停之间的时间差lapTime,用户恢复timer事件时,将timer的delay设置为普通计时器的delay与这个时间差lapTime之间的差值。
下面的代码可以说明“普通计时器”与“精确计时器”的差别:

package {
import flash.display.Sprite;
import flash.utils.Timer;
import flash.utils.getTimer;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.events.Event;
import flash.text.TextFieldAutoSize;
public class pauseTimer extends Sprite {
// the interval between two ticks, in milliseconds
private var interval:int=1000;
// the "normal" timer
private var normalTimer:Timer;
// the "smart" timer
private var smartTimer:Timer;
// just some text field related variables
private var normalTimerText:TextField = new TextField();
private var smartTimerText:TextField = new TextField();
private var normalTextFormat:TextFormat = new TextFormat();
private var smartTextFormat:TextFormat = new TextFormat();
// the "movie paused" screen
private var pausedScreen:pausedMc=new pausedMc();
// variables used to handle the smart timer
private var lapTime:Number=0;
private var currentTimer:Number=0;
public function pauseTimer() {
// nothing interesting, only some layout settings
normalTextFormat.size=24;
normalTextFormat.color=0xA90000;
smartTextFormat.size=24;
smartTextFormat.color=0x00A900;
addChild(normalTimerText);
normalTimerText.autoSize=TextFieldAutoSize.LEFT;
addChild(smartTimerText);
smartTimerText.autoSize=TextFieldAutoSize.LEFT;
addChild(pausedScreen);
smartTimerText.y=50;
// timer initialization
normalTimer=new Timer(interval);
smartTimer=new Timer(interval);
// timer listeners
normalTimer.addEventListener(TimerEvent.TIMER, showNormalTime);
smartTimer.addEventListener(TimerEvent.TIMER, showSmartTime);
// activate/deactivate listeners
addEventListener(Event.ACTIVATE,onActivate);
addEventListener(Event.DEACTIVATE,onDeactivate);
}
private function showNormalTime(e:TimerEvent):void {
// just showing the number of ticks
normalTimerText.text="Normal counter: "+normalTimer.currentCount+" ticks counted";
normalTimerText.setTextFormat(normalTextFormat);
}
private function showSmartTime(e:TimerEvent):void {
// set the delay to "interval" value if it has a different value (it will happen)
if (smartTimer.delay!=interval) {
smartTimer.delay=interval;
}
// saving the current timer
currentTimer=getTimer();
smartTimerText.text="Smart counter: "+smartTimer.currentCount+" ticks counted";
smartTimerText.setTextFormat(smartTextFormat);
}
private function onActivate(e:Event):void {
pausedScreen.visible=false;
// smart timer delay management
if (smartTimer.delay-lapTime>0) {
smartTimer.delay=smartTimer.delay-lapTime;
}
// saving the curren time
currentTimer=getTimer();
// starting the timers
normalTimer.start();
smartTimer.start();
}
private function onDeactivate(e:Event):void {
pausedScreen.visible=true;
// stopping the timers
normalTimer.stop();
smartTimer.stop();
// determining how much time has passed since the last tick
lapTime=(getTimer()-currentTimer);
smartTimerText.text="Smart counter: "+smartTimer.currentCount+" ticks counted and "+(smartTimer.delay-lapTime)+"ms to next tick";
smartTimerText.setTextFormat(smartTextFormat);
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值