Corona的碰撞事件

时间:2014-02-24 01:13:29

标签: lua corona

我想知道如何让我的碰撞在Corona中正常工作。以下是我到目前为止的情况:

local function onLocalCollision( event )

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

    print( "began: " .. event.object1.myName .. " and " .. event.object2.myName )
    test =  display.newRect(screenW - 50, halfH, 100, screenH)


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

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

end

lilPig.collision = onLocalCollision
lilPig:addEventListener( "collision", lilPig )

endOfScreen.collision = onLocalCollision
endOfScreen:addEventListener( "collision", endOfScreen )

如果你能帮助我那会很棒!提前谢谢!

1 个答案:

答案 0 :(得分:1)

我认为,首先应该在COLLISION对象中添加一些属性,例如:

lilPig.myName = "lilPig"

endOfScreen.myName = "endOfScreen"

所以,我们可以在Collision监听器中直接检查它们,如果我们之后有更多的碰撞,我们可以很容易地检查另一个条件。

我们也可以通过RunTime监听器实现它。

--------

RuntimeListener("collision", onCollision )

--------



function onCollision( event)
        if (event.phase == "began") then
        print("COLLISION: ".. event.object1.myName .. " & ".. event.object2.myName)

            if (event.object1.myName == "lilPig" and event.object2.myName == "endOfScreen") or
                   (event.object1.myName == "endOfScreen" and event.object2.myName == "lilPig") then

                            print("Your code of block for collision event")

end