LoginSignup
1
4

More than 5 years have passed since last update.

game engine Defoldのまとめ

Posted at

 最近さわり始めたgame engine Defoldのapiの使い方などについて随時まとめていきます。基本的に自分がゲームを作っていく中で気付いたこと順です。
 公式のtutorialはやって、GameObjectやatlasといった基本的な名称については分かっていること前提です。

キー入力の取り方

input bindingの設定

まずデバイスからの入力に対するアクションの定義をgame.input_bindingに行う。
Input Binding > key_triggerに入力の種類ごとにinput,actionを定義していく。inputはキーの種類(KEY_SPACEなど)。actionはキーを入力したときに発行されるidになるので、一意な名前をつける。jump, shotなど。

input focusするgame objectへのコーディング

キー入力を受け取るgame objectに紐付けたscriptのinit(で、なくてもいいのだが)に以下のコードを記載する。

function init(self)
    -- acquire input focus as soon as the instance has been initialized
    msg.post(".", "acquire_input_focus")
end

 コメントに書いてある通りなのだが、'acquire_input_focus'をpostされたgame object("."はcurrent game object)がキー入力を補足するgame objectとなる。
 acquire_input_focusをpostしたgame objectは、定義されたキー入力があった場合にon_input()が呼び出される。
 on_input()には入力されたキーに対応したaction_id(input bindingでキーのactionに設定した文字列)で呼び出されるので、キー入力のaction_idごとに実行するコードを記載する。

function on_input(self, action_id, action)
    if action_id == hash("jump") then
        -- jumpの時に実行
    end
end

 msg.postについて⇒Defold - API REFERENCE -: msg.post
 acquire_input_focusについて⇒Defold - API REFERENCE - : acquire_input_focus

1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4