I have followed this tutorial on movement and I got this code. But whenever I press the move key it keeps the character moving and when I press the other one it starts to go the other way. How can I fix this?
function init(self)
msg.post(".","acquire_input_focus")
self.runSpeed = 50
self.curAnim = "idle"
msg.post("#sprite", "play_animation", { id=hash("idle") })
self.speed = vmath.vector3()
end
function update(self, dt)
local pos = go.get_position()
if self.speed.x ~= 0 then
pos = pos + self.speed * dt
go.set_position(pos)
if self.curAnim ~= "run" then
msg.post("#sprite", "play_animation", { id=hash("run") })
self.curAnim = "run"
end
end
end
function on_input(self, action_id, action)
if action_id == hash("MOVE_RIGHT") then
self.speed.x = self.runSpeed
sprite.set_hflip("#sprite", false)
end
if action_id == hash("MOVE_LEFT") then
self.speed.x = self.runSpeed * -1
sprite.set_hflip("#sprite", true)
end
end