私はアイソメトリックタイルゲームエンジンを作成し、このコードの速度に問題があることを試みる:私は低い数字にLocation.widthとLocation.heightを設定した場合、それは速い(50 FPS)を実行キャンバスで描画タイルをどのように高速化するには?
$(function() {
var canvas = document.getElementById('GameCanvas');
var context = document.getElementById('GameCanvas').getContext('2d');
var imgObj = new Image();
imgObj.src = 'img/sand_surface.png';
var Game = {
tileScaleX: 64,
tileScaleY: 32,
FPSLimit: 50, // max allowed framerate
realFPS: 0, // real framerate
init: function() {
this.cycle(); // main animation loop
},
cycle: function() {
this.debug(); // print framerate
startTime = new Date; // start fps time
this.clear(); // celar canvas
this.draw(); // draw frame
endTime = new Date; // end fps time
setTimeout(function() {
endTimeWithSleep = new Date; // end fps time with sleep
this.realFPS = 1000/(endTimeWithSleep - startTime);
this.cycle(); // repeat animation loop
}.bind(this), (1000/this.FPSLimit) - (endTime - startTime));
},
debug: function() {
$('.DebugScreen').html('<b>FPS:</b> ' + Math.round(this.realFPS*1)/1);
},
clear: function() {
canvas.width = canvas.width; // clear canvas
},
draw: function() {
Location.drawSurface(); // draw tiles
},
}
var Location = {
width: 60,
height: 120,
drawSurface: function() {
for (y = 0; y < this.height; y++) {
for (x = 0; x < this.width; x++) {
if ((y % 2) == 0) {
rowLeftPadding = 0;
} else {
rowLeftPadding = Game.tileScaleX/2;
}
context.drawImage(imgObj, (x * Game.tileScaleX + rowLeftPadding), y * (Game.tileScaleY/2), Game.tileScaleX, Game.tileScaleY);
}
}
},
}
Game.init(); // start game
});
が、この例では(Location.width = 60
、Location.height = 120
)フレームレートは10fpsで、50fpsが必要です。このスクリプトを高速化する方法はありますか?
好奇心の高まりから、ここで乗算と除算が達成されます: 'Math.round(this.realFPS * 1)/ 1'? –