그래서 HTML과 자바 스크립트로 게임을 만들었습니다. 나는 일종의 플래 세이 버드 쉐어 게임을 만들고 싶었지만 키를 누르면 플레이어의 애니메이션이 정말 망가져 보입니다. 보세요 :HTML5 캔버스의 부드러운 애니메이션
body {
overflow: hidden;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="Style.css"/>
</head>
<body onload="startgame()">
<canvas id="canvas"></canvas>
<script>
canvas.height=window.innerHeight;
canvas.width=window.innerWidth;
function startgame() {
var c = document.getElementById("canvas");
var ctx = c.getContext("2d");
var x = 900;
var y = 300;
var w = 25;
var h = 500;
var yperson = 20;
var xperson = 200;
document.addEventListener("keydown", function() {
yperson -= 150;
});
function updateperson() {
yperson = yperson;
}
setInterval(createobject, 10);
function createobject() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
x -= 1;
yperson += 0.5;
yperson *= 1.003;
ctx.fillStyle = "#009999";
ctx.fillRect(x,y,w,h);
ctx.fillStyle = "black";
ctx.fillRect(xperson,yperson,30,30);
if (x <= 50) {
if (yperson < 280 && xperson === x-30) {
x -= 1;
} else if (yperson > 280){
x += 1;
}
}
}
}
</script>
</body>
</html>
내가 그것을 부드러운 애니메이션 업을 갖고 싶어. 어떤 사람들은 requestanimationframe으로 끝내야한다고 말하는 것을 보았지만 그것을 사용하는 방법을 모른다.
미리 감사드립니다.
// DEFINE OBJECTS UP HERE
var update = function(modifier) {
// update all the object properties
// multiply values that depend on time (like speeds) by modifier
};
var render = function() {
// draw everything
};
var main = function() {
var now = Date.now();
var change = now - then;
update(change/1000); // update based on frame rate, change in milliseconds/second
render();
then = now;
requestAnimationFrame(main);
};
// ADD EVENT LISTENERS HERE
requestAnimationFrame = window.requestAnimationFrame
|| window.webkitRequestAnimationFrame
|| window.msRequestAnimationFrame
|| window.mozRequestAnimationFrame;
// ABOVE CODE GIVES CROSS-BROWSER COMPATIBILITY
var then = Date.now();
main();
requestAnimationFrame 프레임 속도에 따라 루프를 실행하는 브라우저를 알려줍니다
나에게 잘 보이지만 예'requestAnimationFrame'은 디스플레이를 업데이트하는 방법 때문에 약간 부드럽게 보일 것입니다. –
알아. @SpencerWieczorek하지만 키를 누르면 사람이 애니메이션없이 빠르게 올라갑니다. –
위치를 150 픽셀만큼 위로 이동하는 것입니다. 애니메이션을 원하면 위쪽으로 힘을 가하고 상향으로 직접 변환하지 않으려합니다. 이 경우 중간에 애니메이션이 없습니다. –