The event listeners in this game basically turn the movements of the objects on and off. We have already coded the functions that carry out the actions of our game objects to run the level. Now, it's time to activate them using a certain type of events. As you've noticed from the previous chapter, we can add event listeners to display objects or have them run globally.
Collision events within the physics engine occur through Corona's event listener model. There are three new event types, which are as follows:
"collision"
: This event includes phases for "began"
and "ended"
, which signify the moments of initial contact and broken contact. These phases exist for both normal two-body collisions and body-sensor collisions. If you do not implement a "collision"
listener, this event will not fire."preCollision"
: This is an event type that fires right before the objects start to interact. Depending on your game logic, you may wish to detect this event and conditionally override the collision. It may also result in multiple reports per contact and affect the application's performance."postCollision"
: This is an event type that fires right after the objects have interacted. This is the only event in which the collision force is reported. If you do not implement a "postCollision"
listener, this event will not fire.Collisions are reported between pairs of objects and can be detected either globally, using a runtime listener, or locally within an object, using a table listener.
When detected as a runtime event, each collision event includes event.object1
, which contains the table ID of the Corona display object involved.
Here is an example:
local physics = require "physics"
physics.start()
local box1 = display.newImage( "box.png" )
physics.addBody( box1, "dynamic", { density = 1.0, friction = 0.3, bounce = 0.2 } )
box1.myName = "Box 1"
local box2 = display.newImage( "box.png", 0, 350)
physics.addBody( box2, "static", { density = 1.0, friction = 0.3, bounce = 0.2 } )
box2.myName = "Box 2"
local function onCollision( event )
if event.phase == "began" and event.object1.myName == "Box 1" then
print( "Collision made." )
end
end
Runtime:addEventListener( "collision", onCollision )
When detected with a table listener within an object, each collision event includes event.other
, which contains the table ID of the other display object involved in the collision.
local physics = require "physics" physics.start() local box1 = display.newImage( "box.png" ) physics.addBody( box1, "dynamic", { density = 1.0, friction = 0.3, bounce = 0.2 } ) box1.myName = "Box 1" local box2 = display.newImage( "box.png", 0, 350) physics.addBody( box2, "static", { density = 1.0, friction = 0.3, bounce = 0.2 } ) box2.myName = "Box 2" local function onCollision( self, event ) if event.phase == "began" and self.myName == "Box 1" then print( "Collision made." ) end end box1.collision = onCollision box1:addEventListener( "collision", box1 ) box2.collision = onCollision box2:addEventListener( "collision", box2 )