0
requestAnimFrame을 사용하여 간단하게 게임을 만들려고했지만 애니메이션이 작동하지 않아 그 이유를 모르겠습니다. 어쩌면 도움이 될지도 몰라? 여기에 코드입니다 :requestAnimationFrame을 사용한 게임 애니메이션
// requestAnimationFrame() shim by Paul Irish
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function */ callback, /* DOMElement */ element){
window.setTimeout(callback, 1000/60);
};
})();
//Create canvas
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
canvas.width = 640;
canvas.height = 480;
document.body.appendChild(canvas);
// The main game loop
var lastTime;
function main() {
var now = Date.now();
var dt = now - lastTime;
draw();
update(dt);
lastTime = now;
requestAnimFrame(main);
}
main();
function ball(){
this.radius = 5;
this.x = 300;
this.y = 50;
this.vx = 200;
this.vy = 200;
this.ax = 0;
this.ay = 0;
this.color = "red";
this.draw = function(){
ctx.beginPath();
ctx.fillStyle = this.color;
ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
ctx.fill();
};
}
function draw() {
newBall = new ball();
newBall.draw();
}
function update(dt) {
newBall = new ball();
newBall.x += newBall.vx * dt;
}
update(dt)
에서 기능 볼이 이동 나던 이유를 모르겠다는 ...
한 가지는 'dt'가 거의 항상 동일하다는 것입니다. 다른 하나는 매번 새로운 '공'을 만들어내는 것입니다. 그것은 의도입니까, 아니면 당신은 단지 그것을 움직이려고합니까? – kalley