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

**URL:** <https://forum.defold.com/t/animation-during-the-jump-animation-it-plays-walking-animation-if-pressed-right-or-left/43008>\
**Category:** Questions\
**Created:** [February 9, 2019, 8:33am UTC](https://forum.defold.com/t/animation-during-the-jump-animation-it-plays-walking-animation-if-pressed-right-or-left/43008 "2019-02-09T08:33:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hakiim.episal](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/hakiim.episal/32/13562_2.png) [@hakiim.episal](https://forum.defold.com/u/hakiim.episal)\
**Post date:** [February 9, 2019, 8:33am UTC](https://forum.defold.com/t/animation-during-the-jump-animation-it-plays-walking-animation-if-pressed-right-or-left/43008/1 "2019-02-09T08:33:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sicher](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sicher/32/8_2.png) [@sicher](https://forum.defold.com/u/sicher)\
**Post date:** [February 9, 2019, 9:27am UTC](https://forum.defold.com/t/animation-during-the-jump-animation-it-plays-walking-animation-if-pressed-right-or-left/43008/2 "2019-02-09T09:27:20Z")

</div>

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()`: [8 ways movement](https://www.defold.com/examples/input/move/)

---

<div class="post-metadata">

**Author:** ![hakiim.episal](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/hakiim.episal/32/13562_2.png) [@hakiim.episal](https://forum.defold.com/u/hakiim.episal)\
**Post date:** [February 9, 2019, 9:58am UTC](https://forum.defold.com/t/animation-during-the-jump-animation-it-plays-walking-animation-if-pressed-right-or-left/43008/3 "2019-02-09T09:58:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [February 9, 2019, 10:09am UTC](https://forum.defold.com/t/animation-during-the-jump-animation-it-plays-walking-animation-if-pressed-right-or-left/43008/4 "2019-02-09T10:09:20Z")

</div>

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.

```Lua
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.

---

<div class="post-metadata">

**Author:** ![hakiim.episal](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/hakiim.episal/32/13562_2.png) [@hakiim.episal](https://forum.defold.com/u/hakiim.episal)\
**Post date:** [February 9, 2019, 10:38am UTC](https://forum.defold.com/t/animation-during-the-jump-animation-it-plays-walking-animation-if-pressed-right-or-left/43008/5 "2019-02-09T10:38:23Z")

</div>

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