19. Lua v.s Othe Languages
if not carMoving then
-- do something
elseif noGas then
-- do something else
end
if (!carMoving) {
// do something
} else if (noGas) {
// do something else
}
for i = 1,10 do
print(i)
end
for (i=1; i<=10; i++) {
print(i);
}
for j = 100,1,-1 do
print(j)
end
for (j=100; j>0; j++) {
print(j);
}
20. Lua objects are Tables
array = { "a", "b", 100, "hello" }
dictionary = { x=5, y=7, name="Coopermaa" }
t = {}
t[1] = "a"
t["x"] = 5
t.x = 5
key = "x"
t[key] = 5
-----
empty table
numerical index
key index
equivalent property access
-- any type can be a key
t["printCooper"] = function print("Cooper") end
21. Object methods
-- create empty table
local o = {}
-- add method
function o:saySomething(something)
print(something)
end
-- output 'hi'
o:saySomwthing("hi")
22. The ':' is syntactic sugar
-- create empty table
local o = {}
-- add method
function f(self, something)
print(something)
end
o.saySomething = f
-- output 'hi'
o.saySomething(o, "hi")
23. Arrays are 1-based
-- Lua: index begins with 1
local array = {'a', 'b', 'c'}
for i=1,#array do
print(array[i])
end
// Other languages: index begins with 0
array = ['a', 'b', 'c'];
for (var i=0; i<arr.length; i++) {
console.log(array[i]);
}
24. Features of Corona SDK
?
?
?
?
?
?
?
?
?
OpenGL graphics
OpenAL audio
Box2D Physics
Texture atlases, sprites
Networking
GPS, multi-touch, accelerometer, camera, video
Widgets library and native web views, textfields
Services: ads, analytics, IAP
Game networks: Game Center (iOS) and Google Play Game Services
for achievements, leaderboards, real-time multiplayer games
? Corona Cloud (shutdown) : multiplayer support, leaderboards, chat,
push notification, game analytics
88. Event Types
? Runtime Events
– enterFrame - occurs at fps interval (30 or 60)
– system - such as app suspend or exit
– orientation - changes from portrait to landscape or vice-versa
? Local events, such as:
–
–
–
–
–
–
touch (hit) - user touches the device screen / display objects
collision - two physics object collide
timer - a running timer completes its duration
audio - an audio file finishes playing
networkRequest - network requests phase/status/response
location (GPS) - generated by the GPS hardware
98. 設定畫面四邊的位置
-- 設定畫面四邊的位置
-- 這邊是 letterbox 的算法,zoomEven 算法不一樣
local screenTop = 0
local screenBottom = _H
local screenLeft = 0
local screenRight = _W
待會球會移動,碰到邊邊時要讓球切換方向
99. 3
加上動畫讓球移動
-- 加上動畫讓球移動,碰到邊邊時切換方向
function animate(eveent)
local dx = xspeed * xdirection
local dy = yspeed * ydirection
local xNew, yNew = ball.x + dx, ball.y + dy
if xNew > screenRight - radius or xNew < screenLeft + radius then
xdirection = -xdirection
end
if yNew > screenBottom - radius or yNew < screenTop + radius then
ydirection = -ydirection
end
ball:translate(dx, dy)
end
Runtime:addEventListener("enterFrame", animate)
動畫的播放預設是 30 fps,每秒 30 格
104. 1
定義一個畫球的 function
-- 定義畫球的 function,新球都會在畫面中央出現
local function newBall(params)
local xpos = display.contentWidth*0.5
local ypos = display.contentHeight*0.5
local circle = display.newCircle(xpos, ypos, params.radius)
circle:setFillColor(params.r, params.g, params.b)
circle.xdir = params.xdir
circle.ydir = params.ydir
circle.xspeed = params.xspeed
circle.yspeed = params.yspeed
circle.radius = params.radius
return circle
end
105. 2
把畫出來的球放進集合
local collection = {}
-- 畫出三顆球,並把球放進 collection 集合
for i=1,#params do
local ball = newBall(params[i])
collection[#collection + 1] = ball
end
106. 設定畫面四邊的位置
-- 設定畫面四邊的位置, 這邊是 zoomEven 的算法
local d = display -- 變數別名,簡化程式碼
local screenTop = d.screenOriginY
local screenBottom = d.viewableContentHeight + d.screenOriginY
local screenLeft = d.screenOriginX
local screenRight = d.viewableContentWidth + d.screenOriginX
-- config.lua
application =
{
content =
{
width = 320,
height = 480,
scale = "zoomEven"
}
}
107. 3
加上動畫讓球移動
-- 加上動畫讓球移動,碰到邊邊時切換方向
function animate(event)
for _,ball in pairs(collection) do
local dx = ball.xspeed * ball.xdir
local dy = ball.yspeed * ball.ydir
local xNew, yNew = ball.x + dx, ball.y + dy
local radius = ball.radius
if xNew > screenRight - radius or xNew < screenLeft + radius then
ball.xdir = -ball.xdir
end
if yNew > screenBottom - radius or yNew < screenTop + radius then
ball.ydir = -ball.ydir
end
ball:translate(dx, dy)
end
end
Runtime:addEventListener("enterFrame", animate)
117. 1
定義產生箱子的 function
-- 定義產生箱子的 function
function newCrate()
local crate = display.newImage("crate.png")
crate.x = math.random(320)
crate.y = -50; crate.rotation = 5
physics.addBody(crate)
end
x 座標由亂數產生
y 座標設成負的,感覺就像從螢幕外面放入箱子
135. 建立新場景
-- scene1.lua
local storyboard = require "storyboard"
local scene = storyboard.newScene()
function scene:createScene( event )
-- object creation code here
end
function scene:enterScene( event )
-- scene logic goes here
end
scene:addEventListener( "createScene" )
scene:addEventListener( "enterScene" )
return scene
使用 storyboard.newScene() 建立新場景
記得要用 addEventListener() 註冊每個階段的 event listener
137. 清除與移除場景的差別
-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
print( "1: enterScene event" )
-- remove previous scene's view
storyboard.purgeScene( storyboard.getPrevious() )
--[[
INSERT code here (e.g. start timers,
load audio, start listeners, etc.)
]]
end
Mobile Devices 記憶體很珍貴,不用時要「節能減碳」
purgeScene() 是清除 display objects,場景將來還可以再 re-load
removeScene() 是把整個模組 unload
138. Scene Template
--------------------------------------------------------- scenetemplate.lua (節錄)
-------------------------------------------------------
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
-- Called when the scene's view does not exist:
function scene:createScene( event )
local group = self.view
end
-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
end
點按 [Sample Apps > Interface > Storyboard] 打開範例
點按 [File > Show Project Files] 即可找到 scenetemplate.lua
142. 2
titleScreen.lua
-- titleScreen.lua
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
function scene:createScene( event )
local group = self.view
display.newText(group, "開心水族箱", 20, 110, "Arial", 50)
display.newText(group, "Start", 120, 280, "Arial", 40)
end
function scene:enterScene(event)
local function showGameScreen()
-- 切換場景,花 1.2 秒把 titleScreen 往上移
storyboard.gotoScene("gameScreen", "slideUp", 1200)
end
timer.performWithDelay(1000, showGameScreen, 1)
end
scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
return scene
143. 3
gameScreen.lua
-- gameScreen.lua
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local fish = nil
local bubble = audio.loadSound("bubble.wav")
function scene:createScene( event )
local group = self.view
display.newImage(group, "bkg.jpg")
fish = display.newImage(group, "fish.png", 30, 220)
end
function scene:enterScene(event)
-- 讓魚花 3.5 秒鐘的時間移動到右上角
-- 移動時要翻滾 360 度, 快要到達時減速
transition.to(fish, {time=3500, x=250, y=80,
rotation=360, transition=easing.outExpo})
audio.play(bubble)
end
scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
return scene
176. 發 SMS 簡訊
-- 發一則 SMS 簡訊 (不支援夾檔)
local options =
{
to = { "1234567890", "9876543210" },
body = "I scored over 9000!!! " ..
"Can you do better?"
}
native.showPopup("sms", options)
185. 使用自訂字型
-- 使用自訂字型 DS-Digital 顯示 Text
display.newText("12345", 30, 60, "DS-Digital", 100)
display.newText("24680", 30, 180, "DS-Digital", 100)
在 Mac 與 Windows 上,字型名稱可能會不一樣
在 Android 上,檔案名稱 (不加副檔名) 就是字型名稱
186. 讓程式跨平台
-- 同一套字型,在不同平台下名稱可能會不一樣
-- 底下會根據平台調整字型的名稱
local platform = system.getInfo("platformName")
local font = "DS-Digital"
if platform == "Win" then
font = "DS-Digital"
elseif platform == "Android" then
-- filename with no extension, case sensitive
font = "fonts/DS-DIGI"
else
-- Mac and iOS
font = "DS-DIGI"
end
-- 使用自訂字型 DS-Digital 顯示 Text
display.newText("12345", 30, 60, font, 100)
display.newText("24680", 30, 180, font, 100)