To enable the ‘preCollision’ event, you need to manually call the API ‘body:setPreSolveEventsEnabled(true)’.
And Disable the specific collision by return false
in preCollisionEvent.
local platform = display.newRect( 0, 0, 280, 30 )
platform.x, platform.y = display.contentCenterX, display.contentCenterY+80
platform.collType = "passthru"
physics.addBody( platform, "static", { bounce=0.0, friction=0.3 } )
local function preCollisionEvent( self, event )
local collideObject = event.other
if ( collideObject.collType == "passthru" ) then
-- event.contact.isEnabled = false -- box2d v2
return false -- box2d v3
end
return true
end
local ball = display.newCircle( display.contentCenterX, display.contentCenterY-40, 15 )
physics.addBody( ball, "dynamic", { bounce=0.0, radius=20 } )
ball.preCollision = preCollisionEvent
body:setPreSolveEventsEnabled(true)
ball:addEventListener( "preCollision", ball )
We’re replacing ‘postCollision’ with ‘hitCollision’.
You can get detail from this doc.
And also you need to manually call the API ‘body:setPreSolveEventsEnabled(true)’ to enable this event.
local function hitCollisionEvent( self, event )
if e.approachSpeed > 10 then
-- do something
else
-- ..
end
end
local ball = display.newCircle( display.contentCenterX, display.contentCenterY-40, 15 )
physics.addBody( ball, "dynamic", { bounce=0.0, radius=20 } )
ball.hitCollision = hitCollisionEvent
body:setHitEventsEnabled(true)
ball:addEventListener( "hitCollision", ball )