Defold公式にあるwar-battles でDefoldの基本的な使い方を学習してみます。
タイルパレットから配置したい地面を選択し、タイルマップに塗りマップを作成します。
タイルマップ上で、Shift + マウスドラッグすることでタイルマップの範囲コピーが可能なので、よく似たマップは便利な機能を使ってサクサク作りましょう。
からっぽのAtlasが作成されたので、中身を埋めていきます。
これで、シーンにプレイヤーが配置されました。
プレイヤーキャラがマップより後ろの表示されてしまう場合
<sxh cpp; title: player.script> function init(self)
msg.post(".", "acquire_input_focus")
self.moving = false
self.input = vmath.vector3()
self.dir = vmath.vector3(0, 1, 0)
self.speed = 50
end
function final(self)
msg.post(".", "release_input_focus")
end
function update(self, dt)
if self.moving then
local pos = go.get_position()
pos = pos + self.dir * self.speed * dt
go.set_position(pos)
end
self.input.x = 0
self.input.y = 0
self.moving = false
end
function onmessage(self, messageid, message, sender) end
function oninput(self, actionid, action)
if action_id == hash("up") then
self.input.y = 1
elseif action_id == hash("down") then
self.input.y = -1
elseif action_id == hash("left") then
self.input.x = -1
elseif action_id == hash("right") then
self.input.x = 1
end
if vmath.length(self.input) > 0 then
self.moving = true
self.dir = vmath.normalize(self.input)
end
end
function onreload(self) end </sxh> * init で変数の初期化。oninput で入力処理を、update で移動処理を実装している。
<sxh cpp; title: player.script> function init(self)
msg.post(".", "acquire_input_focus")
self.moving = false
self.firing = false -- add
self.input = vmath.vector3()
self.dir = vmath.vector3(0, 1, 0)
self.speed = 50
end
function update(self, dt)
if self.moving then
local pos = go.get_position()
pos = pos + self.dir * self.speed * dt
go.set_position(pos)
end
if self.firing then
local angle = math.atan2(self.dir.y, self.dir.x)
local rot = vmath.quat_rotation_z(angle)
local props = { dir = self.dir }
factory.create("#rocketfactory", nil, rot, props)
end
self.input.x = 0
self.input.y = 0
self.moving = false
self.firing = false -- add
end
function oninput(self, actionid, action)
if action_id == hash("up") then
self.input.y = 1
elseif action_id == hash("down") then
self.input.y = -1
elseif action_id == hash("left") then
self.input.x = -1
elseif action_id == hash("right") then
self.input.x = 1
-- add
elseif action_id == hash("fire") and action.pressed then
self.firing = true
end
if vmath.length(self.input) > 0 then
self.moving = true
self.dir = vmath.normalize(self.input)
end
end </sxh> add と書かれた部分のコードを追加します。 fire キーが押された時に、rocketfactory を生成するスクリプトになっています。
<sxh cpp; title: rocket.script> go.property(“dir”, vmath.vector3())
function init(self)
self.speed = 200
end
function final(self) end
function update(self, dt)
local pos = go.get_position() pos = pos + self.dir * self.speed * dt go.set_position(pos)
end
function onmessage(self, messageid, message, sender) end
function oninput(self, actionid, action) end
function on_reload(self) end
</sxh> dir プロパティを追加。initでロケットの速度を初期化し、updateで移動処理を実装しています。
執筆中…