Lua 碰撞检测不需要';t工作在场景内部:显示
我正在尝试检测Lua 碰撞检测不需要';t工作在场景内部:显示,lua,coronasdk,collision-detection,Lua,Coronasdk,Collision Detection,我正在尝试检测场景: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 )
场景: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
只需检查是否正在使用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(“冲突”,onCollision)运行此代码时,您是否遇到任何错误?只是我使用trans对象时出现的错误,当然,这会发生,因为如果我在侦听器之外删除此块,tans对象仍然没有实例化bot。不,我没有遇到任何错误。我很难理解问题是什么?检测似乎没有触发?或者代码根本不运行?检测不会触发