Hi Coronaville,
I have a problem regarding getting postCollision event. I register my preCollision and postCollision this way:
function foodObject:preCollision(event) -- This enables us to disable collision for top border so we can make it one sided collider -- As in food can come into the screen and go over the top border but after it's over top -- top border, it cannot go through it from bottom and will get deleted. -- print("Just got in FoodPreCollisionHandler") if event.contact then if (event.other.myType == "TopWall") then vx, vy = self:getLinearVelocity() if vy \> 0 then -- Only disable the collision if we are moving upward event.contact.isEnabled = false --disable this specific collision! print("disabling collision") end end else print("IMPORTANT: event does NOT have a 'contact' variable with it!") end end ---------------------------------------------------------------------- function foodObject:postCollision( event ) print( "Remove listeners") self:removeEventListener( "preCollision" ) self:removeEventListener( "postCollision" ) end ---------------------------------------------------------------------- foodObject:addEventListener( "preCollision") foodObject:addEventListener( "postCollision")
I also have a collision handler but since it’s a lot of code, I didn’t put it here.
Strange thing is if I comment the “event.contact.isEnabled = false” line, I get the postCollision event but if I don’t, I won’t get that event!
I tested the sample preCollision and postCollision code and doing the same thing on that code won’t affect the postCollision event:
local physics = require("physics") physics.start() local platform1 = display.newRect( 20, 200, 280, 15 ) platform1.myName = "platform1" physics.addBody(platform1, "static") local platform2 = display.newRect( 20, 320, 280, 15 ) platform2.myName = "platform2" physics.addBody(platform2, "static") local box = display.newRect( 20, 20, 50, 50 ) box:setFillColor(255,255,0,255) physics.addBody(box) local box2 = display.newRect( 100, 20, 50, 50 ) box2:setFillColor(255,0,0,255) physics.addBody(box2) function box:preCollision( event ) local platform = event.other if platform.myName == "platform1" then -- Let box pass through platform 1 event.contact.isEnabled = false print( "preCollision", platform.myName ) end end box:addEventListener( "preCollision" ) function box:postCollision( event ) print( "Remove listeners") self:removeEventListener( "preCollision" ) self:removeEventListener( "postCollision" ) end box:addEventListener( "postCollision" )
Any ideas?
Thanks in advance.