问咎儿:[转]由Timer事件驱动的Flash游戏或者动画的暂停方法
来源:百度文库 编辑:九乡新闻网 时间:2024/04/30 08:35:22
不久之前,我介绍了通过ACTIVATE和DEACTIVATE事件侦测焦点暂停Flash游戏或动画。
在使用Timer事件来驱动Flash游戏或者动画时,这个方法出现了意料之外的问题。
尽管你会认为,我只想需要停止和重新启动timer计时,然后重置内部计时器就可以了。
假如有一个timer事件每秒钟触发一次,如果暂停游戏,然后在小于1秒的时间内容恢复又暂停游戏,那么这个timer事件将无法再次被触发。
解决方法可以这样做,首先记录timer事件上一次触发到Flash暂停之间的时间差lapTime,用户恢复timer事件时,将timer的delay设置为普通计时器的delay与这个时间差lapTime之间的差值。
下面的代码可以说明“普通计时器”与“精确计时器”的差别:
Java代码
在使用Timer事件来驱动Flash游戏或者动画时,这个方法出现了意料之外的问题。
尽管你会认为,我只想需要停止和重新启动timer计时,然后重置内部计时器就可以了。
假如有一个timer事件每秒钟触发一次,如果暂停游戏,然后在小于1秒的时间内容恢复又暂停游戏,那么这个timer事件将无法再次被触发。
解决方法可以这样做,首先记录timer事件上一次触发到Flash暂停之间的时间差lapTime,用户恢复timer事件时,将timer的delay设置为普通计时器的delay与这个时间差lapTime之间的差值。
下面的代码可以说明“普通计时器”与“精确计时器”的差别:
Java代码
- 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);
- }
- }
- }
[转]由Timer事件驱动的Flash游戏或者动画的暂停方法
在EXCEL或者WORD文档中插入FLASH的方法
插入FLASH动画的方法和技巧。
Flash动画添加声音的基本方法
在EXCEL或者WORD文档中插入FLASH的方法_
有趣的FLASH动画
漂亮的flash动画
漂亮的flash动画
(奇特的Flash动画)
找FLASH动画地址的方法(独家秘籍)
在PPT中插入Flash动画的方法0.0.0.
在PPT中插入Flash动画的方法1
引用 找FLASH动画地址的方法(独家秘籍)【原创】
找FLASH动画地址的方法(独家秘籍)【原创】
在PPT中插入Flash动画的四种方法
找FLASH动画地址的方法(独家秘籍)
]经典Flash动画Flash成人游戏
用户故事驱动的软件开发方法
删除文件或者文件夹的另一种方法
精美的flash组合动画
美轮美奂的(flash)动画3
美轮美奂的(flash)动画22
精美的端午节FLASH 动画
几个有趣的Flash动画-