【咸鱼教程】基于系统时间的计时器DateTimer(不受FPS影响)
生活随笔
收集整理的這篇文章主要介紹了
【咸鱼教程】基于系统时间的计时器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一致
| 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影响)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Gson与FastJson比较
- 下一篇: synchronized关键字原理