> function init(self)
> msg.post(".", "acquire_input_focus")
> self.vel = vmath.vector3()
> end
>
> function update(self, dt)
> local pos = go.get_position()
> pos = pos + (self.vel * dt)
> go.set_position(pos)
> self.vel.x = 0
> end
>
> function on_input(self, action_id, action)
>
> if action_id == hash("right") then
> self.vel.x= 150
> sprite.play_flipbook("#sprite", "walk")
> elseif action_id == hash("left") then
> self.vel.x= -150
> sprite.play_flipbook("#sprite", "walk")
> end
> if action_id == hash("right") and action.released then
> self.vel.x= 150
> sprite.play_flipbook("#sprite", "idle")
> elseif action_id == hash("left") and action. released then
> self.vel.x= -150
> sprite.play_flipbook("#sprite", "idle")
> end
> end
This is my code for my character moving. However. whenever I press right or left arrow, the character moves, but does not play “walk” flipbook animation. what is the error?
Hi! Welcome to the forum. Do you have a sprite called “sprite” in your game object? Do you have an animation called “walk” in your atlas?
1 Like
Thank you for the welcome!
Yes, I do. The idle animation plays after button is released, but walk animation does not play while button is being pressed and the character is moving.
The way you have it set up, the sprite will restart the “walk” animation every frame that the left or right keys are pressed. If you have your sprite set up correctly, it should work if you change it so that it only calls sprite.play_flipbook()
when a key is pressed
2 Likes
It worked! I changed it to
function on_input(self, action_id, action)
if action_id == hash("right") then
self.vel.x= 150
elseif action_id == hash("left") then
self.vel.x= -150
end
if action_id == hash("right") and action.pressed then
sprite.play_flipbook("#sprite", "walk")
self.right = true
self.left = false
elseif action_id == hash("left") and action.pressed then
sprite.play_flipbook("#sprite", "walk")
self.left= true
self.right=false
end
if action_id == hash("right") and action.released then
if self.left ~= true then
sprite.play_flipbook("#sprite", "idle")
end
elseif action_id == hash("left") and action. released then
if self.right ~= true then
sprite.play_flipbook("#sprite", "idle")
end
end
end
Thank you for the advice.
2 Likes