この文書の現在のバージョンと選択したバージョンの差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
wiki:defold2:tips:003 [2017/10/29 12:30] step |
wiki:defold2:tips:003 [2018/04/04 22:56] (現在) step |
||
---|---|---|---|
ライン 1: | ライン 1: | ||
- | ====== 公式チュートリアル War battles で学習する ====== | + | ====== 003 公式チュートリアル War battles で学習する ====== |
===== 概要 ===== | ===== 概要 ===== | ||
ライン 128: | ライン 128: | ||
* Scriptには雛形となるコードが記述されているので、これを改造して実装する。 | * Scriptには雛形となるコードが記述されているので、これを改造して実装する。 | ||
- | <sxh cpp; title: player.script> | + | <code> |
function init(self) | function init(self) | ||
msg.post(".", "acquire_input_focus") | msg.post(".", "acquire_input_focus") | ||
ライン 176: | ライン 176: | ||
function on_reload(self) | function on_reload(self) | ||
end | end | ||
- | </sxh> | + | </code> |
* init で変数の初期化。on_input で入力処理を、update で移動処理を実装している。 | * init で変数の初期化。on_input で入力処理を、update で移動処理を実装している。 | ||
* このスクリプトをplayerのGameObjectにAdd Component File から追加します。 | * このスクリプトをplayerのGameObjectにAdd Component File から追加します。 | ||
ライン 199: | ライン 199: | ||
- | <sxh cpp; title: player.script> | + | <code> |
function init(self) | function init(self) | ||
msg.post(".", "acquire_input_focus") | msg.post(".", "acquire_input_focus") | ||
ライン 250: | ライン 250: | ||
end | end | ||
end | end | ||
- | </sxh> | + | </code> |
add と書かれた部分のコードを追加します。 | add と書かれた部分のコードを追加します。 | ||
fire キーが押された時に、rocketfactory を生成するスクリプトになっています。 | fire キーが押された時に、rocketfactory を生成するスクリプトになっています。 | ||
ライン 261: | ライン 261: | ||
* scripts 右クリック -> New -> Script からスクリプトを追加。名前は rocket.script としました。 | * scripts 右クリック -> New -> Script からスクリプトを追加。名前は rocket.script としました。 | ||
- | <sxh cpp; title: rocket.script> | + | <code> |
go.property("dir", vmath.vector3()) | go.property("dir", vmath.vector3()) | ||
ライン 286: | ライン 286: | ||
end | end | ||
- | </sxh> | + | </code> |
dir プロパティを追加。initでロケットの速度を初期化し、updateで移動処理を実装しています。 | dir プロパティを追加。initでロケットの速度を初期化し、updateで移動処理を実装しています。 | ||
ライン 312: | ライン 312: | ||
* rocket.script を編集します。 | * rocket.script を編集します。 | ||
- | <sxh cpp; title: rocket.script> | + | <code> |
function init(self) | function init(self) | ||
self.speed = 200 | self.speed = 200 | ||
ライン 334: | ライン 334: | ||
end | end | ||
- | </sxh> | + | </code> |
実行するとロケットが飛んで1秒後に爆発エフェクトが再生されます。 | 実行するとロケットが飛んで1秒後に爆発エフェクトが再生されます。 | ||
しかしこのままでは爆発エフェクトのゴミが残ってしまいます。エフェクトのゲームオブジェクトが削除される様に修正します。 | しかしこのままでは爆発エフェクトのゴミが残ってしまいます。エフェクトのゲームオブジェクトが削除される様に修正します。 | ||
- | <sxh cpp; title: rocket.script> | + | <code> |
function on_message(self, message_id, message, sender) | function on_message(self, message_id, message, sender) | ||
if message_id == hash("animation_done") then | if message_id == hash("animation_done") then | ||
ライン 344: | ライン 344: | ||
end | end | ||
end | end | ||
- | </sxh> | + | </code> |
on_message にスクリプトを追加します。hash値でチェックしている“animation_done” は“play_animation”が呼ばれたたあと送られてくるゲームエンジンのに内包されているメッセージです。この場合、“play_animation”でのSpriteアニメーションの再生が完了時に“animation_done”が呼ばれる形になっています。その時go.delete()関数でゲームオブジェクトを削除します。こうすることで、爆発アニメーションが終了したあと、rocketゲームオブジェクトが削除されます。 | on_message にスクリプトを追加します。hash値でチェックしている“animation_done” は“play_animation”が呼ばれたたあと送られてくるゲームエンジンのに内包されているメッセージです。この場合、“play_animation”でのSpriteアニメーションの再生が完了時に“animation_done”が呼ばれる形になっています。その時go.delete()関数でゲームオブジェクトを削除します。こうすることで、爆発アニメーションが終了したあと、rocketゲームオブジェクトが削除されます。 | ||
\\ | \\ | ||
ライン 399: | ライン 399: | ||
-- 関数化。爆発アニメーションの再生。 | -- 関数化。爆発アニメーションの再生。 | ||
- | <sxh cpp; title: rocket.script> | + | <code> |
local function explode(self) | local function explode(self) | ||
self.life = 1000 | self.life = 1000 | ||
ライン 428: | ライン 428: | ||
end | end | ||
end | end | ||
- | </sxh> | + | </code> |
爆発アニメーションをexplode 関数にまとめました。on_message 関数で"collision_response"が送られてきた時に、explode で爆発アニメーションを再生し、対象のGameObjectを削除しています。実行するとロケットが戦車に当たると爆発アニメーションと共に消える様になっています。 | 爆発アニメーションをexplode 関数にまとめました。on_message 関数で"collision_response"が送られてきた時に、explode で爆発アニメーションを再生し、対象のGameObjectを削除しています。実行するとロケットが戦車に当たると爆発アニメーションと共に消える様になっています。 | ||
\\ | \\ | ||
ライン 460: | ライン 460: | ||
- ui.gui_scriptを開きます。 | - ui.gui_scriptを開きます。 | ||
- | <sxh lua; title: ui.gui_script> | + | <code> |
function init(self) | function init(self) | ||
- | self.score = 0 | + | -- score の変数を定義し0で初期化します。 |
+ | self.score = 0 | ||
end | end | ||
function on_message(self, message_id, message, sender) | function on_message(self, message_id, message, sender) | ||
+ | -- "add_score"メッセージが返された時に | ||
if message_id == hash("add_score") then | if message_id == hash("add_score") then | ||
+ | -- scoreを加算する。 | ||
self.score = self.score + message.score | self.score = self.score + message.score | ||
+ | -- score ノードを取得して、テキストを更新します。 | ||
local scorenode = gui.get_node("score") | local scorenode = gui.get_node("score") | ||
gui.set_text(scorenode, "SCORE: " .. self.score) | gui.set_text(scorenode, "SCORE: " .. self.score) | ||
end | end | ||
end | end | ||
- | </sxh> | + | </code> |
- | + | - add_score メッセージを発火するためのスクリプトをrocket.gui_scriptに書きます。 | |
- | 執筆中.. | + | <code> |
+ | 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 | ||
+ | </code> | ||
+ | これでロケットで戦車を倒すとスコアが100点加算される様になります。 | ||
+ | ===== 終わり ===== | ||
+ | - 公式チュートリアルはここで終了しています。 | ||
+ | - なんどもトライ&エラーを繰り返してコツを掴むことが大切です。 |