我一直在玩Box2DJS很多,認爲我已經找到了我需要的東西來實現它幾乎所有的東西(例如碰撞檢測掛鉤等)。下面是我寫的一個箱子或圓形圖像代替盒或圓形對象的集成的代碼片段:
if (myBallImage)
{
for (aBody = world.m_bodyList; aBody != null; aBody = aBody.m_next)
{
var jellyObject = aBody.GetUserData();
if (typeof(jellyObject) != "object" || jellyObject == null)
continue;
var position = aBody.GetCenterPosition();
var angle = aBody.GetRotation();
var mass = aBody.GetMass();
var halfheight = 0;
var halfwidth = 0;
for (aShape = aBody.GetShapeList(); aShape != null; aShape = aShape.m_next)
{
halfheight = aShape.GetHalfHeight();
halfwidth = aShape.GetHalfWidth();
}
ctx.save();
ctx.translate(position.x, position.y);
ctx.rotate(angle);
ctx.translate(-halfwidth, -halfheight); // halfwidth, halfheight
if (jellyObject.shape == "MYBALL")
ctx.drawImage(myBallImage, 0, 0, halfwidth*2, halfheight*2);
else
if (jellyObject.shape == "MYBOX")
ctx.drawImage(myBoxImage, 0, 0, halfwidth*2, halfheight*2);
ctx.restore();
}
}
else
{
myBallImage = new Image();
myBallImage.src = 'circle.gif';
myBoxImage = new Image();
myBoxImage.src = 'box.gif';
}
在這裏找到這個線程: http://stackoverflow.com/questions/3796025/fill -svg-path-element-with-a-background-image ...但我嘗試將圖像定義爲圖案,然後將該圖案應用於球迄今尚未運行。我也不太明白那個線程中的響應者是在用第二個位(顯然是調用這個模式並將它應用到svg對象)。 – relic 2012-01-12 19:00:48