I’m trying to get back into Defold after a hiatus and I’m going through your tutorials. Overall, I think they are good, though you do go a bit fast at times. I’m currently stuck on Part 3 going over velocity (timestamp 9:50). I’ve gone through and rechecked my code against that in the video at least a dozen times and they are the same, as far as I can tell. However, when I run my game, the player instantly starts moving to the left, which makes sense to me because the code sets the position to increase by self.velocity in the update function, which is called constantly, as I understand it. Yet, in the video, the player only moves when given input. What am I missing?
My code is below. The only difference should be I named my action_id “move_right” instead of “right”.
local DIRECTION_RIGHT = 1
local DIRECTION_LEFT = -1
local BASE_VELOCITY = 500
function init(self)
msg.post("#", “acquire_input_focus”) – tell this component to acquire input focus
self.velocity = vmath.vector3(0, 0, 0)
end
function walk(self)
self.velocity.x = BASE_VELOCITY * self.direction
end
function flip(direction)
sprite.set_hflip("#sprite", direction < 0)
end
function animate(action)
if action.pressed then
sprite.play_flipbook("#sprite", “run”)
elseif action.released then
sprite.play_flipbook("#sprite", “idle”)
end
end
function fixed_update(self, dt)
local position = go.get_position() – get current game object’s position
position = position + self.velocity * dt
go.set_position(position) – set the position to the current game object
end
function on_input(self, action_id, action)
animate(action)
self.direction = (action_id == hash(“move_right”)) and DIRECTION_RIGHT or DIRECTION_LEFT
walk(self)
flip(self.direction)
end