碰撞检测在场景内不起作用:显示 [英] collision detection doesn't work inside scene:show
本文介绍了碰撞检测在场景内不起作用:显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试检测scene:show
函数内部两个对象的碰撞
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")
还有我的碰撞侦听器
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
推荐答案
只需检查您是否正在使用RuntimeEvent侦听器添加冲突侦听器.
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)
Runtime:addEventListener( "collision", onCollision )
这篇关于碰撞检测在场景内不起作用:显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文