似乎当我在 HTML5 画布上创建形状时,只有我创建的第一个形状实际上有工作碰撞。每个形状都应该相互碰撞,但事实并非如此。
这是运动和碰撞检查代码:
function handleMouseMove(event)
{
var mouse = canvas.getMouseCoordinates(event);
if (mouseIsDown && canvas.selected)
{
if (shapes.length > 0)
{
for (var i = 0; i < shapes.length; i++)
{
canvas.selected.oldX = canvas.selected.x;
canvas.selected.oldY = canvas.selected.y;
canvas.selected.x = canvas.selected.x + (mouse.x - canvas.mouseX);
canvas.selected.y = canvas.selected.y + (mouse.y - canvas.mouseY);
if (canvas.selected != shapes[i] && canvas.selected.collides(shapes[i], canvas.selected))
{
canvas.selected.x = canvas.selected.oldX;
canvas.selected.y = canvas.selected.oldY;
}
canvas.mouseX = mouse.x;
canvas.mouseY = mouse.y;
}
}
}
}
还要注意,其他形状在选择时正确检测到碰撞,但它们不会阻塞(IE oldX 和 x 相等)。上面的代码是唯一改变这些值的地方。