【咸鱼教程】基于系统时间的计时器DateTimer(不受FPS影响)
教程目錄
一 計(jì)時(shí)器簡(jiǎn)介
二 計(jì)時(shí)器實(shí)現(xiàn)
三 Demo下載
一 計(jì)時(shí)器簡(jiǎn)介
在手機(jī)上跑游戲時(shí),可能由于運(yùn)動(dòng)物體過(guò)多,導(dǎo)致幀頻太低,計(jì)時(shí)不準(zhǔn)確。
比如一些倒計(jì)時(shí)的游戲,可能倒計(jì)時(shí)30s,變成了35s。
比如iphone運(yùn)行流暢游戲倒計(jì)時(shí)60s,實(shí)際耗時(shí)60s,而android有點(diǎn)兒慢,倒計(jì)時(shí)60s,實(shí)際耗時(shí)70s。
比如一些物體運(yùn)動(dòng),每幀移動(dòng)1像素,60fps,移動(dòng)60像素,由于卡頓,幀頻降低到40fps,那么實(shí)際這個(gè)物體只移動(dòng)了40像素。
比如在unity中,有兩種幀環(huán)FixedUpdate跟Update,Update每幀執(zhí)行一次,而FixedUpdate固定間隔執(zhí)行一次.
比如...
所以我寫(xiě)了一個(gè)計(jì)時(shí)器,基于系統(tǒng)時(shí)間計(jì)時(shí),不受fps影響。
該工具類(lèi)參考了某位水友的帖子,忘了是哪個(gè)貼了,在此感謝一下...
如圖,在幀頻較低時(shí),egret.Event.ENTER_FRAME和egret.timer計(jì)數(shù)較低,而DateTimer計(jì)數(shù)不受fps影響。
?
二 計(jì)時(shí)器實(shí)現(xiàn)
使用方法和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 | /** ?* 根據(jù)系統(tǒng)時(shí)間的計(jì)時(shí)器 ?* @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{ ????/**以前時(shí)間 */ ????private previous: number; ????/**當(dāng)前時(shí)間 */ ????private curTime: number; ????/**已過(guò)去時(shí)間 */ ????private passTime: number; ????/**累計(jì)時(shí)間 */ ????private accTime: number; ????/**每幀耗時(shí) */ ????public delay: number; ????/**當(dāng)前計(jì)數(shù) */ ????public currentCount:number; ????/**設(shè)置的計(jì)時(shí)器運(yùn)行總次數(shù) */ ????public repeatCount:number; ????? ????????public constructor(delay:number,repeatCount:number = 0) { ????????????super(); ????????????this.delay = delay; ????????????this.repeatCount = repeatCount; ????????} ????????? ????/**開(kāi)始計(jì)時(shí) */ ????public start(){ ????????this.previous = egret.getTimer(); ????????this.accTime = 0; ????????egret.startTick(this.update, this); ????????} ????????? ????/**重置計(jì)時(shí) */ ????????public reset(){ ????????this.previous = egret.getTimer(); ????????this.accTime = 0; ????????this.currentCount = 0; ????????} ????????? ????/**停止計(jì)時(shí) */ ????public stop(){ ???????egret.stopTick(this.update, this); ????????} ????????? ????/**更新時(shí)間 */ ????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下載
轉(zhuǎn)載于:https://www.cnblogs.com/gamedaybyday/p/9219941.html
總結(jié)
以上是生活随笔為你收集整理的【咸鱼教程】基于系统时间的计时器DateTimer(不受FPS影响)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Gson与FastJson比较
- 下一篇: synchronized关键字原理