0

Corona SDK Mobile App Development Tutorial – Part 3

Corona SDK Mobile App Development Tutorial – Part 3

Corona SDK Mobile App Development Tutorial – Part 1

Corona SDK Mobile App Development Tutorial – Part 2

In this tutorial , we are going to look at below topics in Corona SDK Mobile Development.

1) Display text on screen

2) Event Listeners – Identify Tap and Touch events

3) Timers

4) Transitions

More details below.

 1) Display text on screen

local myTextObject = display.newText( "Hello World - I am learning Corona!", 100, 200, "Arial", 50 )

The above statements tells the OS to display text “Hello World – I am learning Corona!” at positions x = 100 and y = 200 with font name as “Arial” and font size = 50

Corona SDK Lua- Display Text

Corona SDK Lua- Display Text

 

2) Event Listeners – Identify Tap and Touch events

It is very important to detect the events in your app. Events are some kind of actions done by the person using the app. Some examples of events done by the user are tapping the screen , touching the screen etc.

Tap events and touch events can be easily detected using Corona.

local object1 = display.newImage("vehicle.png",120,120)
object1.x = 200
object1.y = 100

local object2 = display.newImage("vehicle.png",120,120)
object2.x = 200
object2.y = 400
function function1()
print (" User Tapped")
--local alert = native.showAlert( "Message","User tapped" )
local alert = native.showAlert( "Message", "User Tapped", { "OK", "Learn More" })
end

function function2(event)

if event.phase == "began" then
print( "Moved phase of touch event began." )
elseif event.phase == "ended" or event.phase == "cancelled" then
local alert = native.showAlert( "Message", "User Touched", { "OK", "Learn More" })
end

end

object1:addEventListener( "tap", function1 )
object2:addEventListener( "touch", function2 )

In this example , two objects are created and tap event listener is set on Object1 and touch event listener is set on Object2

If the user taps on the screen on Object1, user sees an alert box saying “User Tapped”

Corona SDK Event Listener Example - Tap Event

Corona SDK Event Listener Example – Tap Event

If the user touches on the screen on Object2, user sees an alert box saying “User Touched”

Corona SDK Event Listener Example - Touch Event

Corona SDK Event Listener Example – Touch Event

 

3) Timers

Timer can be used to call function after specific time or specified number of times.

local function listener( event )
print( "listener called" )
end
timer.performWithDelay( 1000, listener )

This calls the function listener after 1000 ms
We can also specify how many times we want to call this timer function.

local function listener( event )
     print( "listener called" )
end
timer.performWithDelay( 1000, listener, 50 )

This timer calls the function listener 50 times with a gap of 1000ms between each call.

4) Transitions

Transitions are methods/declarations that can used to transition an object on screen from one point to another with a specific speed , specific direction and some other properties.

local object1 = display.newImage("vehicle.png",120,120)
object1.x = 200
object1.y = 100

function listener()
print("Transition completed")
end

transition.to( object1, { time=500, x =250 , y = 300, onComplete=listener } )

This block of code moves/transitions the object1 from x = 200 to x = 250 and displaces the object from y = 100 to y = 300 and once tha transition of the object is done , the listener function “listener” is called.

More info on transitions at http://docs.coronalabs.com/api/library/transition/to.html