나는 등각 타일 게임 엔진을 작성하려고하고이 코드의 속도로 문제가 : 나는 낮은 번호 Location.width 및 Location.height을 설정하면캔버스에서 타일 그리기의 속도를 높이려면 어떻게해야합니까?
$(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
});
는, 다음 빠르게 (초당 50 프레임)을 실행하지만, 이 예 (Location.width = 60
, Location.height = 120
)의 프레임 속도는 10fps이고 50fps가 필요합니다.이 스크립트의 속도를 높이는 방법에 대해 의구심이 있습니까?
호기심에서, 여기서 곱셈과 나눗셈이 성취하는 것 :'Math.round (this.realFPS * 1)/1'? –