corona caravan #1 nagaoka
DESCRIPTION
Corona SDK Sample Code Reading Event.#1: Getting Started.TRANSCRIPT
Corona Caravan NiigataCorona Caravan Niigata#1 Getting Started
ConceptConceptCorona SDK でのアプリ開発を覚えよう
Lua の文法は基本だけサンプルコード読みながら「 Corona のお作法」を目と指で覚える。
MenuMenuCorona SDK とは
◦ http://jp.anscamobile.com/corona/Warming Up
◦ Lua 基礎文法超最速マスターCode Reading
◦ Getting Started Hello World Hello World – Localized Timer Clock Frame Animation-1 Time Animation Frame Animation-2
Warming UpWarming UpLua の文法で大事なことだけ
◦Lua 基礎文法最速マスター http://handasse.blogspot.com/2010/02/lu
a.html
◦↑から更に端折って抜粋
Warming UpWarming UpLua とは
◦超軽量言語◦全部で 200KB 台の言語仕様
覚えることも少ない◦他言語への組み込みで活躍◦ゲーム分野でも強い◦軽量であるが故の高速
でも低機能ではない◦ガーベジコレクション搭載
ε= \_○ !ノ イヤッホーゥ
Warming UpWarming Upprint(hoge)
◦どんな言語にもだいたいある感じのアレ◦コマンドラインな Hello World の代表選手
◦なんだかんだで優秀なデバッグ要員
Warming UpWarming Upコメント
◦ハイフン2つで1行コメント -- コメント行
◦ハイフン2つ+大カッコ2つで複数行コメント --[[
hoge()--]]
コメント始まりをハイフン3つにすると複数行コメントが解除されるという謎テクあり
Warming UpWarming Up型
◦ nil: 非値 (non-value)
◦ boolean: 真偽型◦ number: 数値型◦ string: 文字列型◦ userdata: ユーザーデータ
◦ function: 関数◦ thread: スレッド◦ table: テーブル
例◦ nil◦ true, false◦ 1,2,3,…◦ “Hello World!”◦ ( ゚ Д ゚ ) ?◦ print()◦ ( ゚ Д ゚ ) ?◦ {1, 2, a=10, “hoge”}
Warming UpWarming UpLua にありがちな構文
◦ for 条件 do hoge() end◦ while 条件 do hoge() end◦ if 条件 then hoge() end◦ function() hoge=huga end◦ for キー , 値 in pairs(t) do hoge() end t :テーブル
Warming UpWarming Up代入
◦a = b◦a, b, c = 1, 2, 3◦a, b = b, a --スワップ可
◦local a = 1◦local p = print()◦local t = {1, 2, 3}
Warming UpWarming Upテーブル
◦ t={1,2,3} t[1]→1 t[2]→2 t[3]→3
◦ t={“a”,“b”,“c”} t[1]→”a” t[2]→”b” t[3]→”c” #t→ 3
◦ t={a=1,b=2,c=3} t[“a”]→1 t[“b”]→2 t[“c”]→3 t.a→1 t.b→2 t.c→3
Warming UpWarming Upテーブルでクラスっぽいことする
◦ f = printi = 1a = “hello”
◦ t = {f, i, a} t.i→1 t.a→”Hello” t.f(a)→コンソールに” Hello”
継承とかもできるけどメンドイネストしてさらにそれっぽく
◦ t={a={b=“hello”}, hoge=print}◦ t.a.b→”hello”◦ t.hoge(t.a.b)→コンソールに” hello”
Warming UpWarming Up関数は複数の値を return可
◦f = function()local a,b = 1,2return a,b
end◦local a,b,c = f()
a→1 b→2 c→nil
Warming UpWarming Up真偽値
◦偽となる値 false nil
◦真となる値 true 上記以外の全ての値
0 も真
Warming UpWarming Up論理演算子
◦and 値1 and 値2 真 and 真 → 値2 偽 and 真 → 値1
◦or 値1 or 値2 真 or 偽 → 値1 偽 or 真 → 値2 デフォルト値代入に使われたりする
local a = arg or default↑argが nilなら defaultが a に代入される
Warming UpWarming Up関数の特殊例
◦v:func(a,b) v.func(v,a,b)のシンタックスシュガー
◦宣言の仕方 function v:func(a,b) self.hoge(a)end
オブジェクト自身を表す「 self」を使える
Code ReadingCode Readinghttp://developer.anscamobile.co
m/sample-code/getting-started
Hello WorldHello Worlddisplay.newImage(ファイル名 )display.newText(文字列 , left, top, font, fontsize)
オブジェクトのプロパティオブジェクトのメソッド
Hello World - LocalizedHello World - Localizedテーブルsystemライブラリdisplay.newGroup():insert([index, ]object[, リセットフラグ ])
display.newRoundedRect(left, top, width, height, radius)
TimerTimerevent.countオブジェクトメソッド (t:timer)
◦selftimer.performWithDelay(遅延ミリ秒 , listener[, 繰り返し回数 ])
ClockClockRuntime:addEventListener( “orientation”, リスナ )◦端末傾けると動く!
timer.performWithDelay()◦iterationは -1で無限ループらしい
os.dateで貰える時・分・秒は 1〜9 では 1 ケタなので、 2 ケタに揃えるために 0 を付加したりしている
Frame Animation 1Frame Animation 1りんごの画像の中心点から壁までの一定距離を使うことで、当たり判定を実現
左右の壁にぶつかると x ベクトル、上下の壁にぶつかると y ベクトルを反転し、進行方向を変える。
object:translate(x,y)◦x, yの値の分だけ objectを移動させる◦状態変化の結果を定めない点で、transition.to()とは異なる
Time AnimationTime Animation凄く頑張って物理演算してる
◦Q. 何故 Physics使わないのだろう・・・
◦A. Physicsライブラリ登場以前のサンプル
time-basedというか、タッチ動作を実現するためのイベントリスナの使い方サンプルの色が強い気がする◦確かに物理演算には時間による減衰が表現されているんだけれど・・・
Frame Animation 2Frame Animation 2Frame Animation 1の上手い版パラメータを含んだテーブルの数だけボールが増える
for _,item in ipairs( params ) do◦ジェネリック forでボールを生成していく方法が上手い
ボールの動作自体は Frame Animation1と同じことやってる