I'm making an HTML Canvas game (Tron), and I'm want the game to detect when a player hits the border of the canvas. I have an if statement that checks if the player is in contact with either their own trail or the opponents, and that works fine, but the parameter for being outside of the canvas (y >= 780) doesn't seem to be doing anything. I'll post the relevant code here and if anyone wants to see a JSFiddle, just ask.
function moveDown() {
if(kd.LEFT.isDown() === false && kd.UP.isDown() === false && kd.RIGHT.isDown() === false && ctx.getImageData(x, y+21, 1, 1).data[0] !== 255 && ctx.getImageData(x, y+21, 1, 1).data[2] !== 255) {
y+=10;
ctx.fillStyle = "red";
ctx.fillRect(x,y,20,20);
} else if(ctx.getImageData(x, y+21, 1, 1).data[0] === 255 || ctx.getImageData(x, y+21, 1, 1).data[2] === 255 || y >= 780) {
alert("Blue wins!");
}
}
Aucun commentaire:
Enregistrer un commentaire