教程目录
一 计时器简介
二 计时器实现
三 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. 深度学习 Deep Learning UFLDL 最新Tutorial 学习笔记 5:Softmax Regression

    Softmax Regression Tutorial地址:http://ufldl.stanford.edu/tutorial/supervised/SoftmaxRegression/ 从本节開始 ...

  2. c++ int string互转

    1. ){ monthNumber = "; itoa(i+,iCharArray,); monthNumber += iCharArray; } else { itoa(i+,iCharA ...

  3. ASP利用xhEditor编辑器实现图片上传的功能。

    本人这几天在做一个软件,无意中用到xhEditor在线编辑器,这个编辑器虽然看着比较简单,但功能非常强大,大家可以去官网上查看,废话不说了. 这篇文件主要是实现在ASP环境中利用xhEditor编辑器 ...

  4. 获取GridView中RowCommand的当前索引行(转)

    获取GridView中RowCommand的当前索引行 前台添加一模版列,里面添加一个LinkButton 前台 (如果在后台代码中用e.CommandArgument取值的话前台代码就必须在按钮中设 ...

  5. fdisk mkfs blkid fstab

    fdisk -l  查看系统分区信息 mkfs  制作文件系统mke2fs 制作ext型文件系统mkntfs  制作ntfs型文件系统 e2label  更改ext型文件系统卷标ntfslabel 更 ...

  6. 浮点数向偶数舍入的问题 Round-to-Even for Floating Point

    Round-To-Even在于To-Up , To-Down, To-towards-Zero对比中,在一定数据量基础上,更加精准.To-Up的平均值比真实数值偏大,To-Down偏小.   例如有效 ...

  7. 7 Django系列之关于bootstrap-table插件的简单使用

    preface 我们在前端html页面的时候做表格最常用的就是jinja2渲染,这样的好处是无须引用外部插件,直接使用就行,方便.但是不好的就是前端CSS样式以及其他表格排序筛选功能需要自己写,增加了 ...

  8. Lua基础语法讲解

    Lua 是什么? Lua 是一种轻量小巧的脚本语言,用标准C语言编写并以源代码形式开放, 其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能. Lua 是巴西里约热内卢天主教大学( ...

  9. CStringArray error C2248: 'CObject::CObject' : cannot access private member declared in class

    在开发中将一个字符串分割,并将子字符串保存在CStringArray中,专门写了一个函数,如下: SplitStringToCString(CString str, TCHAR tszSplit, C ...

  10. Python 网络编程相关知识学习

    Python 网络编程 Python 提供了两个级别访问的网络服务.: 低级别的网络服务支持基本的 Socket,它提供了标准的 BSD Sockets API,可以访问底层操作系统Socket接口的 ...