教程目录
一 计时器简介
二 计时器实现
三 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下载

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

  1. .NET/C# 在代码中测量代码执行耗时的建议(比较系统性能计数器和系统时间)

    我们有很多种方法评估一个方法的执行耗时,比如使用性能分析工具,使用基准性能测试.不过传统的在代码中编写计时的方式依然有效,因为它可以生产环境或用户端得到真实环境下的执行耗时. 如果你希望在 .NET/ ...

  2. GDI绘制时钟效果,与系统时间保持同步,基于Winform

    2018年工作之余,想起来捡起GDI方面的技术,特意在RichCodeBox项目中做了两个示例程序,其中一个就是时钟效果,纯C#开发.这个CSharpQuartz是今天上午抽出一些时间,编写的,算是偷 ...

  3. 基于【 centos7】二 || 系统时间与网络时间同步

    # date // 查看系统时间 #hwclock // 查看硬件时间 # yum -y install ntp ntpdate 安装ntpdate工具 # ntpdate cn.pool.ntp.o ...

  4. Visual Studio图形调试器详细使用教程(基于DirectX11)

    前言 对于DirectX程序开发者来说,学会使用Visual Studio Graphics Debugger(图形调试器)可以帮助你全面了解渲染管线绑定的资源和运行状态,从而确认问题所在.现在就以我 ...

  5. IIS7日志中时间与系统时间不一致的原因

    最近在分析web日志,发现IIS7日志中时间与系统时间不一致,即本该上班时间才产生的产并发访问日志,全部发生在凌晨至上班前. 本以为是系统时间设置错误,检查后一切正常.后查询资料,原来是这个原因: 日 ...

  6. C# 获取系统时间及时间格式

    --DateTime 数字型 System.DateTime currentTime=new System.DateTime(); 取当前年月日时分秒      currentTime=System. ...

  7. LR中获取当前系统时间方法

    方法一:使用loadrunner的参数化获取当前时间使用lr的参数化,非常方便,对lr熟悉的各位朋友也能马上上手,时间格式也有很多,可以自由选择.步骤:1.将复制给aa的值参数化2.选中abc,使用右 ...

  8. C语言获取系统时间的几种方式[转]

    C语言获取系统时间的几种方式 C语言中如何获取时间?精度如何? 1 使用time_t time( time_t * timer ) 精确到秒 2 使用clock_t clock() 得到的是CPU时间 ...

  9. C语言得到当前系统时间

    void getTime(){ //获取当前系统时间 time_t tTime;//距离1900年1月1日的秒数 char str[80]; struct tm* stTim;//时间结构 time( ...

随机推荐

  1. 【翻译自mos文章】在RHEL7/OL7上安装Oracle 12.1.0.2的server端或者client时,报须要"compat-libstdc++"包

    在RHEL7/OL7上安装Oracle 12.1.0.2的server端或者client时,报须要"compat-libstdc++"包 来源于: Installation of ...

  2. spark not contain

    参考网址 http://stackoverflow.com/questions/33608526/is-there-a-way-to-filter-a-field-not-containing-som ...

  3. (asp)JScript读写、复制、移动文件 asp也就那回事(4)

    百度博客http://hi.baidu.com/sdink/blog/ 和QQ空间同时更新http://516649425.qzone.qq.com <" CODEPAGE=" ...

  4. asp.net存储过程分页+GridView控件 几百万数据 超快

    存储过程:---亲测275万数据,分页速度N快 ))+' '+@orderid+' from '+@tablename+' '+@tmpOrderid set @sql='select top'+st ...

  5. 查看cp进度,使用watch

    watch -n 1 -d du -sh dir 每隔1s查看当前目录所占空间大小

  6. Spring Boot 处理 REST API 错误的正确姿势

    摘要:如何正确的处理API的返回信息,让返回的错误信息提供更多的含义是一个非常值得做的功能.默认一般返回的都是难以理解的堆栈信息,然而这些信息也许对于API的客户端来说有可能并没有多大用途,并没有多大 ...

  7. 安装 运行yum报错:No module named yum

    报错情况: There was a problem importing one of the Python modulesrequired to run yum. The error leading ...

  8. [转]spring 官方下载地址(Spring Framework 3.2.x&Spring Framework 4.0.x)

    SPRING官方网站改版后,建议都是通过 Maven和Gradle下载,对不使用Maven和Gradle开发项目的,下载就非常麻烦,下给出Spring Framework jar官方直接下载路径: h ...

  9. PHP代码审计笔记--任意文件下载漏洞

    在文件下载操作中,文件名及路径由客户端传入的参数控制,并且未进行有效的过滤,导致用户可恶意下载任意文件.  0x01 客户端下载 常见于系统中存在文件(附件/文档等资源)下载的地方. 漏洞示例代码: ...

  10. centos6.4安装 jupyter-notebook

    自上次发布了文章后有些网友就说不能实现效果,根据自己的实验发现确实有此事,那是因为版本的变化问题.这次基于yum仓库里的jupyter notebook 5.0.0版本实现: 系统:最小化安装[习惯性 ...