首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的游戏循环加速,人物留下痕迹

我的游戏循环加速,人物留下痕迹
EN

Stack Overflow用户
提问于 2017-06-13 18:40:42
回答 1查看 53关注 0票数 0

守则:

代码语言:javascript
复制
// 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) 
}

如果有点长的话很抱歉。

应该发生的是鬼魂以稳定的速度在屏幕周围随机移动。相反,它移动的随机,但越来越快,并留下自己的副本到处。

恢复功能不是每次都要清除屏幕吗?

我的游戏回路弄错了吗?提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-13 18:50:40

代码语言:javascript
复制
function mainLoop(){
  setInterval(mainLoop,100);
}

我认为这个错误是显而易见的。我建议用setTimeout或requestAnimationFrame代替.这个应该移除复本我想这是个光学错觉..。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44529168

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档