mjpez13
mjpez13

Reputation: 1

Collision detection JavaScript malfunction

My program is a collision based game. However the collision detection function malfunctions and increases the score rapidly at random points and ends the game early even if the player is not colliding with anything. I have reviewed my code for several days and tried multiple collision detection functions but i keep getting this issue. I am making this game on code.org app lab and suspect it may possibly be on their end. But i just wanted to know if I'm missing something. Heres a link to my program: 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);
}

Upvotes: 0

Views: 340

Answers (1)

Drew Dahlman
Drew Dahlman

Reputation: 4972

Here is a quick snippet - it may help to break things up and not have everything all inline instead create variables to reference for the objects.

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;
}

Here you want to compare 2 objects like you are but keep things clean to better understand what all is happening.

Upvotes: 0

Related Questions