Game Development Reference
In-Depth Information
The
cancelled phase
is triggered when a touch event is interrupted after it triggers the
began
phase
but does not complete with the
ended
phase. This interruption can be due to an incoming phone call,
a system-triggered message, a notification, and so on.
Let's write some code to display the touch events (what we have just read about):
function handler( event )
print( "touch phase=" .. event.phase .. " at x=" .. event.x .. ", y=" .. event.y)
end
Runtime:addEventListener( "touch", handler )
On running this code, we can look at the terminal window for the output.
Similar to the
touch
event, there is a
tap
event can be handled. The difference between a touch and
tap is that a touch starts with the
began
phase and ends with the
ended
phase. A tap event, on the
other hand, is triggered when the user begins the touch and ends it without moving the touch. The
tap event has a property
numTaps
that indicates the number of taps associated with that touch event.
function onTap( event )
print( "Taps :".. event.numTaps )
end
Runtime:addEventListener( "tap", onTap )
Physics
The feature that brought Corona SDK into the limelight and made it popular with developers was its
ability to integrate physics into its code in about eight lines or so. You can make any display object
into a physics object simply by assigning it as a physics body using the function
physics.addBody()
.
local ground = display.newRect( 0, 40, 320, 20)
On running this code, rectangle is displayed on the screen. In order to make this a
physics
object,
we need to include the physics library. I described in earlier chapters that the you can include
libraries by using the
require
keyword, like so:
local physics = require "physics"
physics.start()
These two lines are of utmost importance: the first one gets a handle on the
physics
library, which
allows us to call the physics-related functions from the
physics
namespace, and the second line
actually starts the physics engine. Making the objects
physics
objects does not make a difference
until
physics.start
is called. This is mostly used in games that allow the user to position the objects
on the screen and then invoke the
physics.start
function to start the physics simulation interacting
with the placed objects. Some examples that use this are Bubble Ball and Angry Orange (not made
with Corona SDK).