Html5 Canvas形状的运动和碰撞,只有第一个形状创造了碰撞?

时间:2013-03-06 15:54:19

标签: javascript html5 html5-canvas collision

似乎当我在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相等)。上面的代码是这些值被更改的唯一地方。

0 个答案:

没有答案