碰撞检测在场景的show函数中无法工作。

我正在尝试在我的scene:show函数中检测两个物体的碰撞

以下是我第一组具有碰撞侦听器的对象

for i = 1, table.maxn(layout.playgrid) do
  physics.addBody( leftholes[i], "static" )
  sceneGroup:insert(3,leftholes[i])
  leftholes[i].name = "hole"
  leftholes[i]:addEventListener( "collision", lis )
  physics.addBody( rightholes[i], "static")
  sceneGroup:insert(3,rightholes[i])
  rightholes[i].name = "hole"
  rightholes[i]:addEventListener( "collision", lis )
  physics.addBody( topholes[i], "static" )
  sceneGroup:insert(3,topholes[i])
  topholes[i].name = "hole"
  topholes[i]:addEventListener( "collision", lis )
  physics.addBody(bottomholes[i], "static")
  sceneGroup:insert(3,bottomholes[i])
  bottomholes[i]:addEventListener( "collision", lis )
  bottomholes[i].name = "hole"
end

这是将与孔物体发生碰撞的对象

hand = display.newImage("assets/hand.png",350,490)
hand.name = "hand"
physics.addBody( hand, "static")

这是我的碰撞侦听器

local function lis(event)
if event.phase == "began" and event.object2.name=="hand" then
print( "detected" )
local function tra( )
  trans = display.newImage('assets/gray.png',indicesToOuterCordinate(layout.finalx,layout.finaly,layout.finalside,false))
  physics.addBody( trans, "static")
  sceneGroup:insert(trans)
  hand:toFront()
end
end
end
点赞
用户2653067
用户2653067

请检查是否使用RuntimeEvent监听器添加了碰撞侦听器。

如果您没有将碰撞添加到Runtime事件监听器中,您将无法被检测到。

local function onCollision( event )

if ( event.phase == "began" ) then

    print( "began: " .. event.object1.myName .. " and " .. event.object2.myName )

elseif ( event.phase == "ended" ) then

    print( "ended: " .. event.object1.myName .. " and " .. event.object2.myName )

end
end

Runtime:addEventListener( "collision", onCollision )

2014-08-02 11:29:46