0
由於某些原因,此碰撞檢測算法不起作用。我期待在發生碰撞時將警報打印到屏幕上,但這不會發生。我相信這與退貨聲明有關,我在這裏錯過了一些明顯的東西嗎?碰撞檢測不起作用HTML5遊戲
// checkHit is called in the game loop
Player.prototype.checkHit = function() {
for (var i = 0; i < enemies.length; i++) {
var colliding = testCollisionEntity(this, enemies[i]);
if (colliding) {
alert('hello');
}
}
};
testCollisionEntity = function (entity1,entity2){
var rect1 = {
x: entity1.x - entity1.width/2,
y: entity1.y - entity1.height/2,
width: entity1.width,
height: entity1.height,
}
var rect2 = {
x: entity2.x - entity2.width/2,
y: entity2.y - entity2.height/2,
width: entity2.width,
height: entity2.height,
}
return testCollisionRectRect(rect1,rect2);
}
testCollisionRectRect = function(rect1,rect2) {
return rect1.x <= rect2.x + rect2.width
&& rect2.x <= rect1.x + rect1.width
&& rect1.y <= rect2.y + rect2.height
&& rect2.y <= rect1.y + rect1.height;
}
是寬度/ x和高度/ y單位相同?它們是像素嗎?它們是一定的(如32)像素數量嗎?你如何獲得/更新它們?當它只需要4個時,你爲什麼要將8條信息傳遞給你的碰撞檢測功能?你確定這些都應該是'&&'嗎?邊界應該是'a && b || c && d'通常。我在想你的邏輯沒有描述可獲得的狀態。 –
謝謝Jared,你讓我知道這一點。非常感謝 – stckpete
我會以答覆的形式發帖,請接受。 –