这是我的jsfiddle链接:http://jsfiddle.net/whZ44/8/。我正在尝试检测球是否碰到了支架的顶部。如果是,那么球的新坐标应该是支架的坐标,就像跳跃游戏一样。但是似乎我的碰撞检测函数没有起作用。
if (Collision(ball, std4)) {
console.log("collision");
ball.y = std2.x;
}
我目前正在使用的碰撞函数,但我不确定它是否适用于我的条件:
function Collision(circle, rect) {
var distX = Math.abs(circle.x - rect.x - rect.w / 2);
var distY = Math.abs(circle.y - rect.y - rect.h / 2);
if (distX > (rect.w / 2 + circle.r)) { return false; }
if (distY > (rect.h / 2 + circle.r)) { return false; }
if (distX <= (rect.w / 2)) { return true; }
if (distY <= (rect.h / 2)) { return true; }
var dx = distX - rect.w / 2;
var dy = distY - rect.h / 2;
return (dx * dx + dy * dy <= (circle.r * circle.r));
}
document.createElement("canvas")
或位图/精灵。http://melonjs.github.io/docs/ http://melonjs.github.io/tutorial/ 地图可以使用Tiled创建 - http://www.mapeditor.org/ - Daniel W.