【咸鱼教程】基于系统时间的计时器DateTimer(不受FPS影响)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了【咸鱼教程】基于系统时间的计时器DateTimer(不受FPS影响),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2605字,纯文字阅读大概需要4分钟。
内容图文
![【咸鱼教程】基于系统时间的计时器DateTimer(不受FPS影响)](/upload/InfoBanner/zyjiaocheng/1099/02856385bd524fc8bfca005847d6b96e.jpg)
教程目录
一 计时器简介
二 计时器实现
三 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一致
1
2
3
|
var
dateTimer:DateTimer =
new
DateTimer(
1000
);
dateTimer.addEventListener(egret.TimerEvent.TIMER,
this
.onDateTimerHandler,
this
);
dateTimer.start();
|
DateTimer源码如下
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
内容总结
以上是互联网集市为您收集整理的【咸鱼教程】基于系统时间的计时器DateTimer(不受FPS影响)全部内容,希望文章能够帮你解决【咸鱼教程】基于系统时间的计时器DateTimer(不受FPS影响)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。