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