碰撞检测不在场景内工作:显示

时间:2014-07-27 18:04:52

标签: lua corona collision-detection

我试图检测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

1 个答案:

答案 0 :(得分:0)

只需检查您是否正在使用RuntimeEvent侦听器添加冲突侦听器。

如果您没有添加与运行时事件侦听器的冲突,则无法检测到。

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

运行时:addEventListener(“collision”,onCollision)