Animation - During the jump animation, it plays walking animation if pressed right or left


#1

Hello everyone, I have an issue where during the jump animation, if i press the right button, the walking animation is playing in the air. How to solve this? I want the animation to play only the jump animation regardless if i press right or left button. Most of the code I have been using right now is based on the example given. Thank you

function on_input(self, action_id, action)
if action.pressed then
	-- key pressed
	-- set movement direction and animation according to key
	if action_id == hash("left") then
		self.direction.x = -1
		play_animation(self, hash("move-left"))
		
	elseif action_id == hash("right") then
		self.direction.x = 1
		play_animation(self, hash("move-right"))
		
	elseif action_id == hash("jump") then
		jump(self)
		play_animation(self, hash("move-jump-right"))
		
	elseif action_id == hash("jump") then
		jump(self)
		play_animation(self, hash("move-jump-left"))
	end
elseif action.released then
	-- key released
	-- clear movement direction
	-- set idle animation if no movement direction
	if action_id == hash("left") then
		self.direction.x = 0
		if vmath.length(self.direction) == 0 then play_animation(self, hash("idle-left")) end
	elseif action_id == hash("right") then
		self.direction.x = 0
		if vmath.length(self.direction) == 0 then play_animation(self, hash("idle-right")) end
	elseif action_id == hash("jump")  then
		abort_jump(self)
		--play_animation(self, hash("fall-right")) 
	
	end
end

end


#2

Note that if you press ”left” and ”jump” at the same time the on_input() function will be called two times during the dame frame. The order of the calls is unknown.

Check out this example which modifies state in on_input() and then chooses what to do with the state changes in update(): https://www.defold.com/examples/input/move/


#3

thanks for helping, yes i already read the example that you provides . I understand that if i press left and jump on the input, the function will be called. However i did not understand the solution to solve issue.


#4

Well, you are currently always playing new animations when you are mid air.
The solution to that is to store a flag which tells you what state your object is in. E.g.

function jump(self)
    self.is_jumping = true
    ...
end

Then, you have to check that before receiving more input. And you have to reset it to false when the jump is done.


#5

thanks for helping. yeah haha you solve my problem! many thanks! you are the best :grinning: