对于动画控制,可能一点误差,大家不会察觉,但如果多次循环累积或网络同步等,大家就会很清楚意识到schedule的误差问题。
首先做一个例子证明一下:
var InaccuracyTestLayer = cc.Layer.extend({ ctor: function () { this._super(); var startTime = new Date().getTime(); var count = 0; this.schedule(function(){ var timePass = new Date().getTime() - startTime; count++; var delta = timePass - (count*100); trace("time pass", timePass, "total delta", delta, "count", count); }, 0.1); this.scheduleUpdate(); }, update: function () { for (var i = 0; i < 100000; i++) { b = 1/0.22222; } } });
帧频越低,变慢得越快。
time pass, 1481, total delta, 381, count, 11 CCDebugger.js:334 time pass, 1608, total delta, 408, count, 12 CCDebugger.js:334 time pass, 1735, total delta, 435, count, 13 CCDebugger.js:334 time pass, 1861, total delta, 461, count, 14 CCDebugger.js:334
那么尝试一下解决问题?
定时器原理:cocos2d-js底层在每一帧计算中,遍历所有定时器,看是否达到触发时间。如果达到则触发该定时器,并把时间重置为当前时间。好了,问题就在于此,“重置为当前时间”。
看看一个新的定时器:
schedule2: function (callback, interval) { var then = Date.now(); interval = interval*1000; this.schedule(function(){ var now = Date.now(); var delta = now - then; if(delta > interval){ then = now - (delta % interval); callback.call(this); } }.bind(this), 0); }
这里核心是then=now-(delta%interval),每一次触发的时候,把误差算到下次触发的控制中。
例如60fps,那么schedule2每16ms触发一次,用户设定了100ms的interval,那么将有16*7=112>100,7帧才触发1次用户的定时器。这里累积了12ms误差,把12ms算到then中。
那么下次将有12+16*6=108>100,只需要96ms就触发第2次用户的定时器,这次提前了4ms,弥补了第1次的误差。
这个定时器经得起考验,即使在低帧频情况下,仍然保持稳定。
var BetterScheduleLayer = cc.Layer.extend({ ctor: function () { this._super(); var startTime = Date.now(); var count = 0; this.schedule2(function(){ var timePass = Date.now() - startTime; count++; var delta = timePass - (count*100); trace("time pass", timePass, "total delta", delta, "count", count); }, 0.1); this.scheduleUpdate(); }, schedule2: function (callback, interval) { var then = Date.now(); interval = interval*1000; this.schedule(function(){ var now = Date.now(); var delta = now - then; if(delta > interval){ then = now - (delta % interval); callback.call(this); } }.bind(this), 0); }, update: function () { for (var i = 0; i < 10000000; i++) { b = 1/0.22222; } } });
输出:
time pass, 3447, total delta, 47, count, 34 CCDebugger.js:334 time pass, 3510, total delta, 10, count, 35 CCDebugger.js:334 time pass, 3637, total delta, 37, count, 36 CCDebugger.js:334 time pass, 3701, total delta, 1, count, 37 CCDebugger.js:334 time pass, 3828, total delta, 28, count, 38 CCDebugger.js:334 time pass, 3955, total delta, 55, count, 39 CCDebugger.js:334
时间: 2024-10-10 08:09:32