この文書の現在のバージョンと選択したバージョンの差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン 最新リビジョン 両方とも次のリビジョン | ||
wiki:defold2:tips:003 [2017/10/23 13:03] step |
wiki:defold2:tips:003 [2017/11/11 13:31] step |
||
---|---|---|---|
ライン 1: | ライン 1: | ||
- | ====== 公式チュートリアル War battles で学習する ====== | + | ====== 003 公式チュートリアル War battles で学習する ====== |
===== 概要 ===== | ===== 概要 ===== | ||
ライン 351: | ライン 351: | ||
===== 敵となる戦車の作成 ===== | ===== 敵となる戦車の作成 ===== | ||
+ | - Assts にtank.go ゲームオブジェクトを作成します。 | ||
+ | - player.atlasを開いて新たにAnimationGroupを作成します。名前はtank-downとします。 | ||
+ | - Add Imageからimageを選択します。「tank/down/1.png」「tank/down/2.png」です。 | ||
+ | - FPSは8を設定しました。 | ||
+ | - tank.goを開いてAdd Component ー> Sprite を追加。Atlasを選択してDefault Animationを「tank-down」としました。 | ||
+ | - main.collectionを開いて戦車を配置します。 | ||
+ | - Outlineを右クリック Add GameObject File.よりtank.goを選択します。フィールドの適当な位置に配置します。 | ||
+ | - Z座標は1.0に設定して、フィールドとなる地面より手前に表示される様にします。 | ||
+ | - 3体くらい配置してみましょう。[Cmd(Ctrl) + C]-> [Cmd(Ctrl) + V]複製できます。 | ||
- | 執筆中... | + | {{:wiki:defold2:tips:2017-10-23_22.18.24.png|}} |
+ | \\ | ||
+ | \\ | ||
+ | \\ | ||
+ | |||
+ | ===== ゲームオブジェクトに当たり判定をつける ===== | ||
+ | |||
+ | ====== 戦車に当たり判定をつける ====== | ||
+ | - tank.go を右クリック -> Add Component -> Collision Object を追加。 | ||
+ | - Type は“Kinematic”。 | ||
+ | - Group を"tanks" に設定。 | ||
+ | - Mask を"rockets" に設定。 | ||
+ | - “collisionobject” を右クリック -> Add Shape -> Box の当たり判定を付けます。 | ||
+ | - Boxのサイズを戦車の見た目に合わせます。 - | ||
+ | {{:wiki:defold2:tips:2017-10-23_23.22.12.png|}} | ||
+ | \\ | ||
+ | \\ | ||
+ | \\ | ||
+ | |||
+ | |||
+ | ====== ロケットに当たり判定をつける ====== | ||
+ | - rocket.go を右クリック -> Add Component -> Collision Object を追加。 | ||
+ | - Type は“Kinematic”。 | ||
+ | - Group を"rockets" に設定。 | ||
+ | - Mask を "tanks"に設定。 | ||
+ | - “collisionobject” を右クリック -> Add Shape -> Box の当たり判定を付けます。 | ||
+ | - Boxのサイズをロケットの見た目に合わせます。 | ||
+ | {{:wiki:defold2:tips:2017-10-23_23.27.09.png|}} | ||
+ | \\ | ||
+ | \\ | ||
+ | \\ | ||
+ | ===== 当たり判定に対するリアクションの実装 ===== | ||
+ | - テスト。 | ||
+ | - rocket.scriptを編集します。 | ||
+ | |||
+ | -- 関数化。爆発アニメーションの再生。 | ||
+ | |||
+ | <sxh cpp; title: rocket.script> | ||
+ | local function explode(self) | ||
+ | self.life = 1000 | ||
+ | go.set_rotation(vmath.quat()) | ||
+ | self.speed = 0 | ||
+ | msg.post("#sprite", "play_animation", { id = hash("explosion") }) | ||
+ | end | ||
+ | |||
+ | function update(self, dt) | ||
+ | local pos = go.get_position() | ||
+ | pos = pos + self.dir * self.speed * dt | ||
+ | go.set_position(pos) | ||
+ | |||
+ | self.life = self.life - dt | ||
+ | |||
+ | if self.life < 0 then | ||
+ | explode(self) | ||
+ | end | ||
+ | end | ||
+ | |||
+ | function on_message(self, message_id, message, sender) | ||
+ | if message_id == hash("animation_done") then | ||
+ | go.delete() | ||
+ | -- Add | ||
+ | elseif message_id == hash("collision_response") then | ||
+ | explode(self) | ||
+ | go.delete(message.other_id) | ||
+ | end | ||
+ | end | ||
+ | </sxh> | ||
+ | 爆発アニメーションをexplode 関数にまとめました。on_message 関数で"collision_response"が送られてきた時に、explode で爆発アニメーションを再生し、対象のGameObjectを削除しています。実行するとロケットが戦車に当たると爆発アニメーションと共に消える様になっています。 | ||
+ | \\ | ||
+ | \\ | ||
+ | \\ | ||
+ | ===== UIの実装 ===== | ||
+ | スコアの表示や、クリア用のUIを作ります。 | ||
+ | - Assets/mainフォルダを右クリック -> New -> Font からフォントを追加。名前は[text.font]とします。 | ||
+ | - text.font を開いて[04font.ttf]を選択します。 | ||
+ | - Assets/mainフォルダを右クリック -> New -> Guiを選択。名前は[ui.Gui]とします。 | ||
+ | - ui.Guiを開いてOutLineからFonts を選択し右クリック -> Add ー> text.font を選択します。 | ||
+ | - Nodesを選択し右クリック -> Add -> Text を追加。名前は[score]とします。テキスト内容は“SCORE: 0”を設定しておきます。 | ||
+ | - Textを画面の左上に配置しておきます。 | ||
+ | |||
+ | {{:wiki:defold2:tips:2017-10-24_7.57.59.png|}} | ||
+ | \\ | ||
+ | \\ | ||
+ | - Assets/mainフォルダを右クリック -> New -> Gui Scriptを追加。名前は[ui.gui_script] | ||
+ | - ui.gui ファイルを開いて、Outlineの"Guiを選択した状態で表示されるプロパティのScriptに先程作成した[ui.gui_script]を設定します。 | ||
+ | - main.collection.を開いて、Outlineから右クリック -> Add Game Object ー> ゲームオブジェクトの名前は"gui"とします。 | ||
+ | - "gui"ゲームオブジェクトを右クリック -> Add Component File から"ui.gui"を選択します。 | ||
+ | |||
+ | {{:wiki:defold2:tips:2017-10-29_21.26.11.png|}} | ||
+ | |||
+ | 画面にスコアが表示されました。次はスコアを加算していくためのスクリプトを書いていきます。 | ||
+ | \\ | ||
+ | \\ | ||
+ | |||
+ | |||
+ | ===== スコアの更新 ===== | ||
+ | - ui.gui_scriptを開きます。 | ||
+ | |||
+ | <sxh javascript; title: ui.gui_script> | ||
+ | function init(self) | ||
+ | -- score の変数を定義し0で初期化します。 | ||
+ | self.score = 0 | ||
+ | end | ||
+ | |||
+ | function on_message(self, message_id, message, sender) | ||
+ | |||
+ | -- "add_score"メッセージが返された時に | ||
+ | if message_id == hash("add_score") then | ||
+ | -- scoreを加算する。 | ||
+ | self.score = self.score + message.score | ||
+ | -- score ノードを取得して、テキストを更新します。 | ||
+ | local scorenode = gui.get_node("score") | ||
+ | gui.set_text(scorenode, "SCORE: " .. self.score) | ||
+ | end | ||
+ | end | ||
+ | </sxh> | ||
+ | |||
+ | - add_score メッセージを発火するためのスクリプトをrocket.gui_scriptに書きます。 | ||
+ | <sxh javascript; title: rocket.script> | ||
+ | function on_message(self, message_id, message, sender) | ||
+ | if message_id == hash("animation_done") then | ||
+ | go.delete() | ||
+ | -- Add | ||
+ | elseif message_id == hash("collision_response") then | ||
+ | explode(self) | ||
+ | go.delete(message.other_id) | ||
+ | -- スコアを100点追加。 | ||
+ | msg.post("/gui#ui", "add_score", {score = 100}) | ||
+ | end | ||
+ | end | ||
+ | </sxh> | ||
+ | これでロケットで戦車を倒すとスコアが100点加算される様になります。 | ||
+ | ===== 終わり ===== | ||
+ | - 公式チュートリアルはここで終了しています。 | ||
+ | - なんどもトライ&エラーを繰り返してコツを掴むことが大切です。 |