at0m1c n00b
at0m1c n00b

Reputation: 66

Collision Event in Corona

I am wondering how to make my collision work correctly in Corona. Here is what I have so far:

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 )

If you could help me that would be great! Thanks in advance!

Upvotes: 0

Views: 246

Answers (1)

anshul-systematix
anshul-systematix

Reputation: 416

I think, firstly you should add some property with your COLLISION objects, like:

lilPig.myName = "lilPig"

endOfScreen.myName = "endOfScreen"

so, we can directly check them in Collision listener and also if we have more Collision's after that, we can easily check by another condition.

we can also implement it by RunTime listener.

--------

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

Upvotes: 1

Related Questions