Character movement animation bug with multiple key presses (SOLVED)

First of all, sory for my english, (I’m brazilian). My character moves when a key is pressed, for that i used the comand lines from the engine tutorial; at my own i put a sprite changing function, when the code percepts the movment of the character at the axes “X” or “Y,” it executes “MOVEMENT” animations: UP, DOWN , LEFT or RIGHT while the key is pressed, when it’s released executes the “STOP” animations: UP, DOWN , LEFT or RIGHT.

The bug apears when I’m pressing 2 keys and release 1 of them or if I "click"while I’m walking with the character, the program stops the “MOVEMENT animation” and executes the “STOP animation”.

hi! Why don’t you post your code? We can help you out.

1 Like

I REDUCE MY CODE TO SIMPLYFI THE VIEW

function update(self, dt)
	--função deslocamento
	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_input(self, action_id, action)

	--animação de movimento
	if action.pressed == true then
		if  action_id == hash("up") then
			msg.post("#sprite","play_animation", {id = hash("walkB")})
		elseif action_id == hash("down") then
			msg.post("#sprite","play_animation", {id = hash("walk")})
		end

	end
	
	--animação de estática
	if action.released == true then 
		if animacao == 1 then
			msg.post("#sprite","play_animation", {id = hash("stopB")})
		elseif animacao == 2 then
			msg.post("#sprite","play_animation", {id = hash("stop")})
		end	
	end
	
	--movimentação do personagem
	if action_id == hash("up") then
		self.vel.y = velocidade
		animacao = 1
	elseif action_id == hash("down") then
		self.vel.y = -velocidade
		animacao = 2
	end
	
end

Try something like this:

Nb. I know this is not perfect but it is a reasonable solution that will help someone learn.

function init(self)
	self.up = false
	self.down = false
	self.left = false
	self.right = false
end
function on_input(self, action_id, action)--animação de movimento
if action.pressed == true then
	if  action_id == hash("up") then
		self.up = true
		msg.post("#sprite","play_animation", {id = hash("walkB")})
	elseif action_id == hash("down") then
		self.down = false
		msg.post("#sprite","play_animation", {id = hash("walk")})
	end
elseif action.released == true then
	if  action_id == hash("up") then
		self.up = false
		if self.up == false and self.down == false then 
			msg.post("#sprite","play_animation", {id = hash("stopB")})
		end
	elseif action_id == hash("down") then
		self.down= false
		if self.up == false and self.down == false then 
			msg.post("#sprite","play_animation", {id = hash("stop")})
		end
	end
end
1 Like
  1. Create four variables, self.up, self.down, self.left, self.right.

  2. Set each variable to true when that key is pressed

  3. Set each variable to false when that key is released

  4. When any key is released, we check that all variables are false before playing the “stop” animation.

2 Likes

Thanks man !!! You helped me a lot!!! :grinning:

1 Like

hey, i’m just glad there was a question i could answer on this forum !

3 Likes