【咸鱼教程】基于系统时间的计时器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

时间: 2024-08-30 03:29:27

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

【原】定时器与系统时间

问题:--------------------------------------------------------------------------------用户反馈一些定时活动提前开启或者延后开启1) 登录服务器,查看时间确实慢了或者快了.总之是有几台服务器时间不准确了.2) 查看代码是使用的ScheduledExecutorService.scheduleAtFixedRate,Java的API,不至于这里存在Bug3) 查看log4j日志输出发现:    12点的定时活动,之前的[

C语言获取系统时间的几种方式

C语言获取系统时间的几种方式 2009-07-22 11:18:50|  分类: 编程学习 |字号 订阅 C语言中如何获取时间?精度如何? 1 使用time_t time( time_t * timer ) 精确到秒2 使用clock_t clock() 得到的是CPU时间 精确到1/CLOCKS_PER_SEC秒3 计算时间差使用double difftime( time_t timer1, time_t timer0 )4 使用DWORD GetTickCount() 精确到毫秒5 如果使用

系统时钟 硬件时钟 系统时间 硬件时间 ntpd ntpdate

在Linux下,默认情况下,系统时间和硬件时间,并不会自动同步.在Linux运行过程中,系统时间和硬件时间以异步的方式运行,互不干扰.硬件时间的运行,是靠Bios电池来维持,而系统时间,是用CPU tick来维持的,相互独立. 在系统开机的时候,会自动从Bios中取得硬件时间,设置为系统时间. ntpd:平滑矫正时间,起初64秒矫正一次,后续逐步减少.且有保护功能,与源时间服务器差异过大,会停止矫正. ntpdate:跃变方式矫正时间,对时序依赖严重的应用程序,如数据库,可能会出现2次相同的时间

C语言获取当前系统时间

void getTime(){ //获取当前系统时间 time_t tTime;//距1900年1月1日的秒数 char str[80]; struct tm* stTim;//时间结构 time(&tTime); stTim = localtime(&tTime); strftime(str,sizeof(str),"%Y%m%d%T",stTim);//格式化时间 printf(str); } 格式化时间的格式 %a 星期几的简写 %A 星期几的全称 %b 月分的简

如何解决Ubuntu与Windows双系统时间不同步

导读 不知道有没朋友跟我一样是 Ubuntu 和 Windows 双系统?今天有朋友问到我,当他从 Ubuntu 系统重新启动到 Windows 时,会发现 Windows 中的时间变了,他问我有没办法修复?其实我刚开始使用 Ubuntu 和 Windows 双系统时就遇到这个问题.下面我们就来解释一番. 为什么Ubuntu和Windows双系统会有时间差 之所以 Windows 与 Ubuntu 双系统之间有时间差,是因为这两个系统使用了不同的方式来识别硬件时钟(Hardware Clock)

C#中的系统时间获取问题

C#获取当前系统时间 2010-01-02 16:24 --DateTime 数字型 System.DateTime currentTime=new System.DateTime(); 取当前年月日时分秒      currentTime=System.DateTime.Now; 取当前年     int 年=currentTime.Year; 取当前月     int 月=currentTime.Month; 取当前日     int 日=currentTime.Day; 取当前时    

C语言得到当前系统时间

void getTime(){ //获取当前系统时间 time_t tTime;//距离1900年1月1日的秒数 char str[80]; struct tm* stTim;//时间结构 time(&tTime); stTim = localtime(&tTime); strftime(str,sizeof(str),"%Y%m%d%T",stTim);//格式化时间 printf(str); } 格式化时间的格式 %a 星期几的简写 %A 星期几的全称 %b 月分的

[转]C语言获取系统时间

C语言中如何获取时间?精度如何? 1 使用time_t time( time_t * timer ) 精确到秒2 使用clock_t clock() 得到的是CPU时间 精确到1/CLOCKS_PER_SEC秒3 计算时间差使用double difftime( time_t timer1, time_t timer0 )4 使用DWORD GetTickCount() 精确到毫秒5 如果使用MFC的CTime类,可以用CTime::GetCurrentTime() 精确到秒6 要获取高精度时间,

linux 修改服务器系统时间

前言 因项目需求需要修改统一系统时间 详情配置如下: 在CentOS 7里面有一个命令timedatectl可以帮助我们修改服务器的时区. 1. 查看服务器里的时间设置 timedatectl ,它等同于 timedatectl status : 2. 了解 timedatectl 命令的各个参数: 3. 设置时间 下面看下CentOS修改服务器系统时间 linux安装完毕后,一般都是国外的世界,一点都不方便设置任务,或者导致网站获取本地的时间错乱,所以就需要把服务器的时间改为和本地时间一致,也