collision detection doesn't work inside scene:show

泪湿孤枕 提交于 2019-12-24 23:19:36

问题


I'm trying to detect collision on two object inside my scene:show function

This is my first set of objects which have a collision listener on it

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

and this is the object which will collision with the hole object

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

and that my collision listener

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:


Just check whether your are adding your collision listener with the RuntimeEvent listener .

If you does not add your collision with runtime event listeners your will not be detected .

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 )



来源:https://stackoverflow.com/questions/24983950/collision-detection-doesnt-work-inside-sceneshow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!