-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookbook.lua
More file actions
453 lines (384 loc) · 10.8 KB
/
cookbook.lua
File metadata and controls
453 lines (384 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
--移除statusBar
display.setStatusBar(display.HiddenStatusBar)
--放置圖片
local image = display.newImageRect("imageName.png", 480, 320)
image.x = 240
image.y = 160
--製作按鈕(要先把ui.lua放進)
local ui = require("ui")
local onButtonTouched = function(event)
if event.phase == "press" then
print"just pressed sound button"
end
if event.phase == "release" then
print"just released sound button"
end
end
local myButton = ui.newButton{
defaultSrc = "buttonImage.png",
defaultX=51,
defaultY=224,
overSrc = "buttonImagePressed.png",
overX=51,
overY=224,
onEvent = onButtonTouched,
id="myButton1",
text="",
font = "Helvetica",
textColor = {255,255,255,255},
size = 16,
emboss = false
}
myButton.x = 37
myButton.y = 131
--播放聲音、暫停播放、釋放聲音記憶體
audio.loadStream() --load背景音樂
audio.loadSound() --load音效
audio.play()
audio.pause()
audio.resume()
audio.dispose()
audioTrack = nil
--函數定義
local functionName = function()
-- 要做的事
end
--for迴圈
for i=1,10 do
print(i)
end
--while迴圈
local i=1
while i<=10 do
print(i)
i=i+1
end
--if判斷1
if something then
-- 要做的事
end
--if判斷2
if something then
-- 要做的事
elseif something else then
-- 要做的事
end
--if判斷3
if something then
-- 要做的事
else
-- 要做的事
end
--生成有次序的table1
local fruitBag = {}
fruitBag[1] = "apple"
fruitBag[2] = "banana"
fruitBag[3] = "mango"
print(fruitBag[1])
--生成有次序的table2
local fruitBag ={"apple","banana","mango"}
print(fruitBag[1])
--生成無次序的table
local fruitBag={
red="apple",
yellow="banana"
}
fruitBag.green = "mango"
print(fruitBag.red)
print(fruitBag["red"])
--table的數量
#table
--table 加入元素
table.insert(tableName,position,addElement)
--table 移除元素
table.insert(tableName,position)
--有次序的table列舉元素
for i=1, #fruitName do
print(fruitName[i])
end
--無次序的table列舉元素
for key,value in pairs(tableName) do
end
--改變圖形位置
object.x
object.y
--改變圖形大小
object.xScale --2的話是2倍,1的話是1倍
object.yScale
--圖形旋轉
object.rotation --角度
--圖形可見
object.isVisible --true or false
--圖形透明度
object.alpha --1到0,0是完全透明
--畫圓形
local myCircle = display.newCircle( 100, 100, 30 )
myCircle:setFillColor(128,128,128)
myCircle.strokeWidth = 5
myCircle:setStrokeColor(128,0,0) -- red
--畫方形
local myRectangle = display.newRect(0, 0, 150, 50)
myRectangle.strokeWidth = 3
myRectangle:setFillColor(140, 140, 140)
myRectangle:setStrokeColor(180, 180, 180)
--畫圓角方形
local myRoundedRect = display.newRoundedRect(0, 0, 150, 50, 12)
myRoundedRect.strokeWidth = 3
myRoundedRect:setFillColor(140, 140, 140)
myRoundedRect:setStrokeColor(180, 180, 180)
--畫多邊形
local star = display.newLine( 0,-110, 27,-35 )
star:append( 105,-35, 43,16, 65,90, 0,45, -65,90, -43,15, -105,-35, -27,-35, 0,-110 )
star:setColor( 255, 102, 102, 255 )
star.width = 3
--移動圖層順序
object:toFront()
object:toBack()
--移動東西,或是改變東西的狀態、變形
transition.to(somethingToMove,
{time=1200, x=250, transition = easing.outExpo,
onComplete = function() justDoSomeThing = false end})
easing.inExpo()
easing.inOutExpo()
easing.inOutQuad()
easing.inQuad()
easing.linear()
easing.outExpo()
easing.outQuad()
--過一段時間要執行某函式的寫法,用timer.performWithDelay()
local doSomething = function()
print("do something")
end
timer.performWithDelay(2000,doSomething)
--觸碰事件
moveMyCar = function(event)
if event.phase == "began" then
transition.to(car,{time=800, x=event.x, y = event.y})
end
end
Runtime:addEventListener("touch", moveMyCar)
--加上觸摸事件間聽器
local justTouchScreen = function(event)
--do something here, event.phase=="began" or "ended"
end
Runtime:addEventListener("touch", onSceneTouch)
--各種滑動手勢偵測
local justTouchScreen = function(event)
if event.phase == "ended" then
if event.xStart < event.x and (event.x - event.xStart)>=30 then
print("swipe right")
car.x = car.x+10
return true
elseif event.xStart > event.x and (event.xStart - event.x)>=30 then
print("swipe left")
car.x = car.x-10
return true
end
if event.yStart < event.y and (event.y - event.yStart)>=30 then
print("swipe down")
car.y = car.y+10
return true
elseif event.yStart > event.y and (event.yStart - event.y)>=30 then
print("swipe left")
car.y = car.y-10
return true
end
end
end
Runtime:addEventListener("touch", onSceneTouch)
--單單為某一物體加上觸碰監聽器1
local justTouchCar = function(event)
--do something here, event.phase=="began" or "ended"
end
car:addEventListener("touch", justTouchCar)
--單單為某一物體加上觸碰監聽器2
function car:touch(event)
--do something here, event.phase=="began" or "ended"
end
car:addEventListener("touch", car)
--Endless Running Game
--object.enterFrame = scrollSomething //不同的背景、不同的速度
--movieClip
local movieclip = require("movieclip")
local car
car = movieclip.newAnim({"MyCar1.png","MyCar2.png","MyCar3.png",
"MyCar4.png","MyCar5.png","MyCar6.png",
"MyCar7.png","MyCar8.png","MyCar9.png","MyCar10.png"})
car:setSpeed(.4)
car:setDrag{drag=true}
car:play()
car.x = 83
car.y = 379
--不停放大縮小的按鈕
local function startBtnScaleUp()
local startBtnScaleDown = function()
transition.to(startBtn,{time=150,xScale=1,yScale=1,onComplete= startBtnScaleUp})
end
transition.to(startBtn,{time=150,xScale=1.06,yScale=1.06,onComplete= startBtnScaleDown})
end
startBtnScaleUp()
--支援iphone5
--config.lua
local thisDeviceHeight = 480
if(system.getInfo("model") == "iPhone") or (system.getInfo("model") == "iPod touch") then
local isIPhone5 = (display.pixelHeight >960)
if isIPhone5 then
thisDeviceHeight = 568
end
end
application =
{
content =
{
width = 320,
height = thisDeviceHeight,
scale = "zoomStretch",
fps = 30,
antialias = true,
imageSuffix =
{
["@2x"] = 1.8,
},
},
}
--程式裡處理
local isIPhone5
checkOutIfItsIPhone5 = function()
if display.contentScaleX ==0.5
and display.contentScaleY == 0.5
and display.contentWidth == 320
and display.contentHeight == 568 then
isIPhone5 = true
end
end
drawBackground = function()
if isIPhone5 then
local background = display.newImageRect("BackgroundiPhone5.png",320,568)
background.x = 160
background.y = 284
else
local background = display.newImageRect("Background.png",320,480)
background.x = 160
background.y = 240
end
end
--移除物體
object:removeSelf()
--物理引擎使用
local physics = require "physics"
physics.start()
physics.addBody()
object:applyForce()
--物理物體碰撞
onCollision = function(event)
if event.phase == "began" then
--do something here
end
end
Runtime:addEventListener("collision", onCollision)
--storyboard
local storyboard = require("storyboard")
local scene = storyboard.newScene()
local screenGroup
--畫面沒到螢幕上時,先呼叫createScene,負責UI畫面繪製
function scene:createScene(event)
screenGroup = self.view
--要做什麼事寫在這邊
end
--畫面到螢幕上時,呼叫enterScene,移除之前的場景
function scene:enterScene(event)
--要做什麼事寫在這邊
end
--即將被移除,呼叫exitScene,停止音樂,釋放音樂記憶體
function scene:exitScene()
--要做什麼事寫在這邊
end
--下一個畫面呼叫完enterScene、完全在螢幕上後,呼叫destroyScene
function scene:destroyScene(event)
--要做什麼事寫在這邊
end
scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("destroyScene", scene)
return scene
--storyboard到別的場景
local setting ={
effect = "slideLeft",
time = 300,
}
storyboard.gotoScene("otherScene", setting)
--判斷語系
local language = "en"
local thisOS=system.getInfo("platformName")
if thisOS=="Android" then
print("Don't support Android")
else
language = userDefinedLanguage or system.getPreference("ui","language")
if language ~="zh-Hant" then
language = "en"
end
end
--加入不同語系圖片 coverTitle = display.newImageRect("CoverTitle_"..language..".png",318,124)
--判斷是否有網路
local isMyNetworkReachable
local http = require("socket.http")
local myUrl = "http://tw.yahoo.com"
local response = http.request(myUrl)
if response ==nil then
isMyNetworkReachable = false
else
isMyNetworkReachable = true
end
--WebView
local myWebView = native.newWebView(0,0,320,412)
myWebView:request("http://appsgaga.com/AndroidMobileMiniWebSite/mobile.html")
--用完webView要記得丟棄
myWebView:removeSelf()
myWebView = nil
--用自己的文字
require('bmf')
local myleftLabelFont = bmf.loadFont('LeftLabelFont.fnt')
local leftLabel = bmf.newString(myleftLabelFont,"someString")
--存取資料
saveRecord = function(strFileName, tableValue)
--will save speified value to specified file
local theFile = strFileName
local theValue = tableValue
local path = system.pathForFile(theFile, system.DocumentsDirectory)
-- io.open opens a file at path; returns nil if no file found
local file = io.open(path,"w+")
if file then
file:write(theValue)
io.close(file)
end
end
loadRecord = function(strFileName)
--will load specified file, or create new file if it doesn't exist
local theFile = strFileName
local path = system.pathForFile(theFile, system.DocumentsDirectory)
--io.open opens a file at path; returns nil if no file found
local file = io.open(path,"r")
if file then
--read all contents of file
local myValue = file:read( "*a" )
print("returnTable form loadRecord= " .. myValue)
io.close(file)
return myValue
else
--create file because it doesn't exist yet
local file = io.open(path,"w+")
file:write("99.99")
io.close(file)
local saveTable = "99.99"
return saveTable
end
end
--字串長度
string.len()
---切分字串
string.sub("某個字串",1,2)
--數字轉字串
tostring()
--字串轉數字
tonumber()