我想在我目前正在使用的JavaScript程序中进行碰撞检测工作,但我无法弄清楚它为什么会在这种奇怪的坐标中触发。 X 50 Y 199JavaScript碰撞检测不起作用
如果你们中的任何一位都能很好的帮助,那将会非常感谢。这是我的代码。基于Y轴我说你的if语句
var game = {};
game.fps = 50;
game.playerX = 50;
game.playerY = 50;
game.draw = function() {
c = document.getElementById('canvas');
ctx = c.getContext("2d");
clearCanvas();
//PLAYER
ctx.fillStyle = "blue";
ctx.fillRect(game.playerX, game.playerY, 50, 50);
//ENEMY
ctx.fillStyle = "green";
ctx.fillRect(200, 200, 50, 50);
//Coords
ctx.font = "20px Arial";
ctx.fillStyle = "red";
ctx.fillText(game.playerX, 400, 480);
ctx.fillText(game.playerY, 450, 480);
};
game.update = function() {
document.onkeydown = function() {
switch (window.event.keyCode) {
case 87:
//up
--game.playerY;
break;
case 83:
//down
++game.playerY;
break;
case 65:
//left
--game.playerX;
break;
case 68:
//right
++game.playerX;
break;
}
};
//COLLISION DETECTION
if (game.playerX <= 200 && game.playerX <= 250 && game.playerY >= 200 && game.playerY <= 250) {
alert("it worked!");
game.playerX = 400;
}
//END OF COLLISION DETECTION
};
game.run = function() {
game.update();
game.draw();
};
game.start = function() {
game._intervalId = setInterval(game.run, 1000/game.fps);
};
game.stop = function() {
clearInterval(game._intervalId);
};
啊谢谢我看到我做了什么 – user2580555