【咸鱼教程】基于系统时间的计时器DateTimer(不受FPS影响)

教程目录
一 计时器简介
二 计时器实现
三 Demo下载




一 计时器简介
在手机上跑游戏时,可能由于运动物体过多,导致帧频太低,计时不准确。
比如一些倒计时的游戏,可能倒计时30s,变成了35s。
比如iphone运行流畅游戏倒计时60s,实际耗时60s,而android有点儿慢,倒计时60s,实际耗时70s。
比如一些物体运动,每帧移动1像素,60fps,移动60像素,由于卡顿,帧频降低到40fps,那么实际这个物体只移动了40像素。
比如在unity中,有两种帧环FixedUpdate跟Update,Update每帧执行一次,而FixedUpdate固定间隔执行一次.
比如...


所以我写了一个计时器,基于系统时间计时,不受fps影响。


该工具类参考了某位水友的帖子,忘了是哪个贴了,在此感谢一下...

如图,在帧频较低时,egret.Event.ENTER_FRAME和egret.timer计数较低,而DateTimer计数不受fps影响。
 


二 计时器实现


使用方法和egret.Timer一致

[Actionscript3] 纯文本查看 复制代码
1
2
3
var dateTimer:DateTimer = new DateTimer(1000);
dateTimer.addEventListener(egret.TimerEvent.TIMER, this.onDateTimerHandler, this);
dateTimer.start();




DateTimer源码如下

[Actionscript3] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
 * 根据系统时间的计时器
 * @author chenkai
 * 2016/12/30
 * Example:
 * var dateTimer:DateTimer = new DateTimer(1000);
 * dateTimer.addEventListeners(egret.TimerEvent.TIMER, this.onTimerHandler, this);
 * dateTimer.addEventListeners(egret.TimerEvent.TIMER_COMPLETE, this.onTimerComplete, this);
 * dateTimer.reset();
 * dateTimer.start();
 */
class DateTimer extends egret.EventDispatcher{
    /**以前时间 */
    private previous: number;
    /**当前时间 */
    private curTime: number;
    /**已过去时间 */
    private passTime: number;
    /**累计时间 */
    private accTime: number;
    /**每帧耗时 */
    public delay: number;
    /**当前计数 */
    public currentCount:number;
    /**设置的计时器运行总次数 */
    public repeatCount:number;
     
        public constructor(delay:number,repeatCount:number = 0) {
            super();
            this.delay = delay;
            this.repeatCount = repeatCount;
        }
         
    /**开始计时 */
    public start(){
        this.previous = egret.getTimer();
        this.accTime = 0;
        egret.startTick(this.update, this);
        }
         
    /**重置计时 */
        public reset(){
        this.previous = egret.getTimer();
        this.accTime = 0;
        this.currentCount = 0;
        }
         
    /**停止计时 */
    public stop(){
       egret.stopTick(this.update, this);
        }
         
    /**更新时间 */
    private update():boolean{
        this.curTime = egret.getTimer();
        this.passTime = this.curTime - this.previous;
        this.previous = this.curTime;
        this.accTime += this.passTime;
        while(this.accTime >= this.delay) {
            this.accTime -= this.delay;
            this.currentCount++;
            if(this.repeatCount > 0 && (this.currentCount == this.repeatCount)){
                this.dispatchEvent(new egret.TimerEvent(egret.TimerEvent.TIMER_COMPLETE));
                this.stop();
            }
             
            this.dispatchEvent(new egret.TimerEvent(egret.TimerEvent.TIMER));
        }
        return false;
        }
         
         
}






Demo下载

原文地址:https://www.cnblogs.com/gamedaybyday/p/9219941.html