You are on page 1of 23

TM

www.anscamobile.com

Whats Corona A simple, yet powerful framework allowing rapid mobile cross platform application development.

www.anscamobile.com

TM

Corona 102 Functions & EventListeners


www.anscamobile.com
TM

Corona framework based on Lua

www.anscamobile.com

TM

Corona Lua resources http://ww.lua.org http://www.lua.org/lua-l.html http://developer.anscamobile.com/ http://en.wikipedia.org/wiki/Lua_

www.anscamobile.com

TM

Corona Without functions


local text = display.newText("hello",100,200,"Helvetica",24); textObj:setTextColor(r,g,b);

Disadvantages: When program begins to get too big, difficult to manage code reusing code becomes hard to break apart debugging becomes a nightmare no sense of where program starts

www.anscamobile.com

TM

Corona With Functions:


local function STextColor(textObj, r,g,b) textObj:setTextColor(r,g,b); end local function main() local text = display.newText("hello",100,200,"Helvetica",24); STxtColor(text,255,0,0); end main() Advantages: easier to manage code easier to re-use code on other programs as well easier to debug since you can narrow down where error occurs a sense of where program starts www.anscamobile.com
TM

Corona Functions:
First-class values. Passed by value. Stored in tables. Returned from functions. Global and local. called from other packages

www.anscamobile.com

TM

Corona Function Definitions:


function name(args) body return [values] end Global Function local function name(args) body return [values] end function local to file scope f = function (args) body return [values] end Anonymous function function ( ... ) body return values] end (...) indicates variable args and {...} places them in a table where they processed in standard way.

www.anscamobile.com

TM

Corona More function definitions:


function obj:name(args) body return [values] end Object function getting extra args and self

www.anscamobile.com

TM

Corona Calling functions:


foo(args) simple call. returning zero or more values. t.f(args) calling function store in field f of table t local function f(mode, value) body end local function with two arguments r = f { value =3.14159, mode=auto} Calling a function with table, args can be passed by name. function f(t) body end arguments accessed as t.value, t.mode

www.anscamobile.com

TM

Corona EventListeners:
Coronas event listeners are part of the Corona framework and not part of the Lua programming language.
http://developer.anscamobile.com/content/events-and-listeners

www.anscamobile.com

TM

Corona EventListeners:
Event listeners allow Coronas display objects and program scope to register for specific events to respond to.
http://developer.anscamobile.com/content/events-and-listeners

www.anscamobile.com

TM

Corona EventListeners:
To setup an event listener you must attach an addEventListener. register a function to listen to the event. Optional, remove the event listener after use.
http://developer.anscamobile.com/content/events-and-listeners

www.anscamobile.com

TM

Corona EventListeners:
There are two types of event listeners Runtime dispatched by the system Display-object self-explanatory.
http://developer.anscamobile.com/content/events-and-listeners

www.anscamobile.com

TM

Corona Runtime Event Listeners:


local function onEnterFrame(event) print (event.name) end Runtime:addEventListener(enterFrame,onEnterFrame) http://developer.anscamobile.com/content/events-and-listeners

www.anscamobile.com

TM

Corona Display Object Event Listeners:


local button = display.newImage(button.png,15,25); local function button:touch(event) print (event.name) end button:addEventListener(touch,button) http://developer.anscamobile.com/content/events-and-listeners

www.anscamobile.com

TM

Listeners
local listener = function(event) print( event.name, event.time ) end Runtime:addEventListener("enterFrame", listener)

www.anscamobile.com

TM

Event Types
enterFrame system orientation accelerometer GPS heading touch timer ...
www.anscamobile.com
TM

Touch + Multitouch
x y xStart yStart phase id time
www.anscamobile.com
TM

cicaza @ anscamobile dot com twitter.com/ansca facebook.com/ansca developer.anscamobile.com/forums http://developer.anscamobile.com/code/ http://developer.anscamobile.com/resources/apis/


www.anscamobile.com
TM

Corona SDK
Code Less. Play More.

You might also like