碰撞检测JavaScript故障

时间:2018-03-21 15:38:45

标签: javascript collision-detection

我的程序是基于碰撞的游戏。然而,即使玩家没有碰撞任何东西,碰撞检测功能也会在随机点发生故障并迅速提高分数并提前​​结束游戏。我已经审查了几天我的代码,并尝试了多个碰撞检测功能,但我不断遇到这个问题。我在code.org app lab上制作这个游戏,并怀疑它可能在他们的结尾。但我只是想知道我是否遗漏了什么。 以下是我的计划的链接:https://studio.code.org/projects/applab/4gT5jkTadBOpofcLX__AvAeb0R3keRYLobVqNhHYaqM

var collectedSouls = 0;
var timer = 30;
onEvent("startBtn", "click", function(event) {
  setScreen("Gamescreen");
  playSound("Windows XP startup.mp3", false);
});
onEvent("Begin", "click", function(event) {
  onEvent("Gamescreen", "keydown", function(event) {
    var x = 0;
    var y = 0;
    if (event.key == "Down") {
      y = 3;
    } else if ((event.key == "Up")) {
      y = -3;
    } else if ((event.key == "Left")) {
      x = -3;
    } else if ((event.key == "Right")) {
      x = 3;
    }
    setPosition("player", getXPosition("player") + x, getYPosition("player") + y);
    wrapAround("player");
  });
  timedLoop(1000, function() {
    setText("Timer", timer);
    timer = timer - 1;
    if (timer <= 0) {
      setScreen("GameOver");
    }
  });
});
onEvent("Begin", "click", function(event) {
  timedLoop(1, function() {
    moveObject("spirit1", randomNumber(-10, 10), randomNumber(-10, 10));
    moveObject("spirit2", randomNumber(-10, 10), randomNumber(-10, 10));
    moveObject("spirit3", randomNumber(-10, 10), randomNumber(-10, 10));
    moveObject("spirit4", randomNumber(-10, 10), randomNumber(-10, 10));
    moveObject("spirit5", randomNumber(-10, 10), randomNumber(-10, 10));
    moveObject("spirit6", randomNumber(-10, 10), randomNumber(-10, 10));
    moveObject("spirit7", randomNumber(-10, 10), randomNumber(-10, 10));
  });
});
function moveObject(object, xMove, yMove) {
  var x = getXPosition(object) + xMove;
  var y = getYPosition(object) + yMove;
  setPosition(object, x, y);
  collision(getXPosition("player"), getYPosition("player"), getProperty("player", "width"), getProperty("player", "height"), getXPosition(object), getYPosition(object), getProperty(object, "width"), getProperty(object, "height"));
  if (collision()=== true) {
    hideElement(object);
    collectedSouls = collectedSouls + 1;
    if (collectedSouls >= 7) {
        setScreen("Victory");
      }
  }
  wrapAround(object);
}
function collision(x1, y1, w1, h1, x2, y2, w2, h2) {
    w2 += x2;
    w1 += x1;
    if (x2 > w1 || x1 > w2) return false;
    h2 += y2;
    h1 += y1;
    if (y2 > h1 || y1 > h2) return false;
    return true;
}
function wrapAround(object) {
  var objx = getXPosition(object);
  var objy = getYPosition(object);
  var Width = getProperty(object, "width");
  var Height = getProperty(object, "height");
  if (objx < 0 - Width / 2) {
    objx = 320 - Width / 2;
  } else if ((objx > 320 - Width / 2)) {
    objx = 0 - Width / 2;
  } else if ((objy < 0 - Height / 2)) {
    objy = 450 - Height / 2;
  } else if ((objy > 450 - Height / 2)) {
    objy = 0 - Height / 2;
  }
  setPosition(object, objx, objy);
}

1 个答案:

答案 0 :(得分:0)

这是一个快速片段 - 它可能有助于解决问题并且不会将所有内容全部内联,而是创建变量以引用对象。

function collision(x1, y1, w1, h1, x2, y2, w2, h2) {
    w2 += x2;
    w1 += x1;
    if (x2 > w1 || x1 > w2) return false;
    h2 += y2;
    h1 += y1;
    if (y2 > h1 || y1 > h2) return false;
    return true;
}

在这里,你想要像你一样比较2个对象,但要保持清洁,以便更好地了解所发生的一切。