Corona
Trang 1www.anscamobile.com
Trang 2What’s Corona
A simple, yet powerful framework allowing rapid
mobile cross platform application development.
Trang 3Corona 102
Functions &
EventListeners
Trang 4Corona framework based on Lua
Trang 5Corona Lua resources
http://ww.lua.org http://www.lua.org/lua-l.html http://developer.anscamobile.com/
http://en.wikipedia.org/wiki/Lua_
Trang 6Corona 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
Trang 7Corona 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
Trang 8Corona Functions:
Trang 9Corona Function Definitions:
( ) indicates variable args and { } places them in a table where
they processed in standard way
Trang 10Corona More function definitions:
Trang 11Corona 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)
Trang 12Corona EventListeners:
Corona’s event listeners are part of the
Corona framework and not part of the Lua
programming language.
http://developer.anscamobile.com/content/events-and-listeners
Trang 13Corona EventListeners:
and program scope to register for specific
events to respond to.
http://developer.anscamobile.com/content/events-and-listeners
Trang 15Corona 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
Trang 16Corona Runtime Event Listeners:
local function onEnterFrame(event)
print (event.name)
end
Runtime:addEventListener(“enterFrame”,onEnterFrame)
http://developer.anscamobile.com/content/events-and-listeners
Trang 17Corona 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
Trang 18local listener = function(event)
print( event.name, event.time )
end
Runtime:addEventListener("enterFrame", listener)
Trang 21cicaza @ anscamobile dot com
Trang 22Corona SDK
Code Less Play More.