物体通过关节定位。

我不知道如何在标题中最好描述这个问题,但我会向您展示图片来说明我的意思。

这就是我的问题

这就是我想做的事情

(1)图片1显示了我遇到的问题;

(2)图片2显示了我想要实现的内容。

——问题——

正如您所见,我试图让这3个块对齐,除了那个 extrudes 出来的尖刺。当我在 corona 中设置这个时,它会按高度对齐图像。

这是我的 spawn 函数:

function createBlock(event)
  b = display.newImageRect("images/Spike.png", 37,80)
  b.x = display.contentWidth + 100
  b.y = math.random(2) == 1 and display.contentCenterY -75 or display.contentCenterY +40
  b.rotation = math.random(2) == 1 and 0 or 180
  b.name = 'block'
  physics.addBody( b, "static", physicsData:get("Spike"))
  blocks:insert(b)
end

编辑:

function check( event )
   if b.rotation == 180 then
   b.y = math.random(2) == 1 and display.contentCenterY - 80 or display.contentCenterY + 30
   end
 end
点赞
用户869951
用户869951

如果物体的高度为H,尖头的高度为S(因此3个方块的高度为H-S),并且您将它们当前放置在Y位置,其中Y朝向屏幕底部增加,物体的原点位于底部最低的方块(尖头相反),它们的默认方向是向上,那么:

  • 将具有面向上的尖的方块定位于Y + H处;它们的尖端将位于Y处,而尖底位于Y + S处。
  • 将具有面向下的尖的块定位在Y + S,然后旋转180度。块的最上部边缘将位于Y + S,这是您想要的。

如果第一段中的任何条件不同,您将需要进行相应的调整,但是希望这可以说明如何解决。

更新:

可能更好的方法是将锚点移动到中间方块的中心。默认情况下,锚点在0.5处,因此将其放置在第2个和第3个方块之间(假设距离尖的最远的方块是“第一个方块”)。由于您的块由4个单元格(3个方块和一个尖)组成,因此您可以执行以下操作:

function createBlock(event)
  local H = 80
  local b = display.newImageRect("images/Spike.png", 37, H)
  b.x = display.contentWidth + 100 -- NOTE: weird, doesn't this put the obj off screen?
  local cellH = H/4 -- assume all four cells in a Block have this height
  b.anchorY = 0.5 - 0.25/2 -- each cell is 0.25 of height, need half that, away from spike
  b.rotation = math.random(2) == 1 and 0 or 180 -- will rotate around the new anchorY
  local posY = display.contentCenterY - 75
  b.y = math.random(2) == 1 and posY or posY + 115
  b.name = 'block'
  physics.addBody( b, "static", physicsData:get("Spike"))
  blocks:insert(b)
end

请注意,根据您创建图像的方式,b.anchorY可能是b.anchorY = 0.5 + 0.25/2,但它应该是+或- 1/8。

2014-09-03 12:51:48