我正在製作一個簡單的畫布遊戲,並且我正在使用requestAnimationFrame(Paul Irish的版本)作爲gameloop。我在遊戲中使用了javascript倒計時(顯示秒和100秒),但它不以正確的速度倒計時。我知道這與遊戲的刷新率有關,因此計數器會更新每一幀,而不是每隔100秒。我該如何解決?使用requestAnimationFrame在畫布中使用JS倒數計時器
這裏的計時器對象:
/**
* The timer as an object
*/
function Timer(position, time) {
this.position = position || new Vector(150,210);
this.time = time || 6000;
}
Timer.prototype = {
start: function() {
console.log('start running');
this.initial = this.time; //time in 100'ths of seconds
this.count = this.initial;
this.counter; //10 will run it every 100th of a second
clearInterval(this.counter);
//this.counter = setInterval(this.timer, 10);
this.timer();
},
timer: function() {
//console.log(this.count);
this.count--;
var res = this.count/100;
//Show counter in canvas
return 'Tid kvar: ' + res.toPrecision(this.count.toString().length) + ' sekunder';
},
draw: function(ct) {
if(this.initial === undefined){this.start();} //Start the timer
ct.save();
if(this.count <=0){ //Remove timer if timer has reached 0
ct.clearRect(this.position.x, this.position.y, Racetrack.innerTrackWidth, Racetrack.innerTrackHeight);
return false;
} else { //draw timer
ct.save();
ct.font = 'bold 3em arial';
ct.fillStyle = 'orange';
ct.fillText(this.timer(), this.position.x, this.position.y);
}
ct.restore();
},
}
而且gameloop和部分調用定時器:
var init = function(canvas) {
timer = new Timer(new Vector(160,210), 3000);
}
var render = function() {
ct.clearRect(0,0,width,height);
ship.draw(ct);
racetrack.draw(ct);
//draw timer or results if timer reached 0
timer.draw(ct) !=false ? timer.draw(ct) : endFrame.draw(ct);
};
var gameLoop = function() {
var now = Date.now();
td = (now - (lastGameTick || now))/1000; // Timediff since last frame/gametick
lastGameTick = now;
if(timer.draw(ct) == false) { //stop the game if timer has reached 0.
cancelRequestAnimFrame(gameLoop);
console.log('Time\'s up!');
} else { //Otherwise, keep the game going.
requestAnimFrame(gameLoop);
}
update(td);
render();
};
我也嘗試以此爲timer-對象,但是在調試this.count
顯示號碼第一個循環,未定義第二個循環和NaN每個循環之後(我不確定這將解決時序問題的任何方式?):
function Timer(position, time) {
this.position = position || new Vector(150,210);
this.time = time || 6000;
}
Timer.prototype = {
start: function() {
console.log('start running');
this.initial = this.time; //time in 100'ths of seconds
this.count = this.initial;
this.counter; //10 will run it every 100th of a second
clearInterval(this.counter);
this.counter = setInterval(this.timer, 10);
this.timer();
},
timer: function() {
console.log(this.count);
this.count--;
},
getTime: function() {
var res = this.count/100;
return 'Tid kvar: ' + res.toPrecision(this.count.toString().length) + ' sekunder';
},
draw: function(ct) {
if(this.initial === undefined){this.start();} //Start the timer
ct.save();
if(this.count <=0){ //Remove timer if timer has reached 0
ct.clearRect(this.position.x, this.position.y, Racetrack.innerTrackWidth, Racetrack.innerTrackHeight);
return false;
} else { //draw timer
ct.save();
ct.font = 'bold 3em arial';
ct.fillStyle = 'orange';
ct.fillText(this.getTime(), this.position.x, this.position.y);
}
ct.restore();
},
}
requestAnimationFrame將時間以毫秒爲單位傳遞給它作爲第一個參數調用的函數。 '函數myLoop(time){requestAnimationFrame(myLoop)}'使用'setInterval'特別是在這麼短的時間間隔內會要求麻煩,因爲任何延遲瀏覽器1/100秒的事情最終都會試圖追趕和如果您有一段時間需要渲染的動畫會導致瀏覽器崩潰。避免使用'setInterval'來比任何短於瀏覽器的東西。 – Blindman67
@ Blindman67任何建議如何以另一種方式解決這個問題? – Sluggo