Heya, I’ve got this issue I’ve been trying to solve for hours. I went through most of the topics regarding movement, but I haven’t really managed to come up with a solution.
At first what I wanted to do was make my player sprite move when I touch the screen, but instead I went with a simpler solution and created some gui buttons to control it.
Left and right seem to be working alright, but I can’t get him to come down to its original position when I press jump, plus if I tap jump and then slide my my thumb across the left or right button simultanously then it just starts moving in the air, in either direction.
I’ve decided to use on_message instead of input as it seemed to be easier.
I really started using defold only a short while ago, same with lua so any help is appreciated.
function init(self)
msg.post(".", "acquire_input_focus")
self.vel = vmath.vector3()
end
function update(self, dt)
local anim = hash("player_idle")
if self.vel.x > 0 then
anim = hash("player_walk")
elseif self.vel.x < 0 then
anim = hash("player_walk")
elseif self.vel.y > 0 then
anim = hash("player_jump")
elseif self.vel.y < 0 then
anim = hash("player_jump")
end
if anim ~= self.current_anim then
msg.post("#player", "play_animation", { id = anim })
self.current_anim = anim
end
local pos = go.get_position()
pos = pos + self.vel * dt
go.set_position(pos)
self.vel.x = 0
self.vel.y = 0
end
function on_message(self, message_id, message, sender)
if message_id == hash("move_right") then
self.vel.x = -150
elseif message_id == hash("move_left") then
self.vel.x = 150
elseif message_id == hash("jump") then
self.vel.y = 1500
elseif message_id == hash("jump_down") then
self.vel.y = -1500
end
end