守则:
// create stuff
var ghObj = {
x : 0,
y : 0
}
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var ghost = new Image();
ghost.src = "ghost.png"
//define variables
var ghostMove = function () {
ghObj.x+=(Math.floor(Math.random() * 9 - 4))
console.log(ghObj.x)
ghObj.y+=(Math.floor(Math.random() * 9 - 4))
}
var ghostCheck = function () {
if (ghObj.x<0) {
ghObj.x=0
}
if (ghObj.x>390) {
ghObj.x=390
}
if (ghObj.y<0) {
ghObj.y=0
}
if (ghObj.y>390) {
ghObj.y=390
}
}
var drawIm = function (sprite, position) {
ctx.save();
ctx.translate(position.x, position.y);
ctx.drawImage(sprite, 0, 0, sprite.width, sprite.height, 0, 0, sprite.width, sprite.height);
ctx.restore();
};
// begin "game" when ghost is loaded
ghost.onload = function() {
mainLoop()
}
// main loop
function() {
ghostMove()
ghostCheck()
drawIm(ghost, ghObj)
setInterval(mainLoop, 1000)
}如果有点长的话很抱歉。
应该发生的是鬼魂以稳定的速度在屏幕周围随机移动。相反,它移动的随机,但越来越快,并留下自己的副本到处。
恢复功能不是每次都要清除屏幕吗?
我的游戏回路弄错了吗?提前谢谢。
发布于 2017-06-13 18:50:40
function mainLoop(){
setInterval(mainLoop,100);
}我认为这个错误是显而易见的。我建议用setTimeout或requestAnimationFrame代替.这个应该移除复本我想这是个光学错觉..。
https://stackoverflow.com/questions/44529168
复制相似问题