# Having trouble with multiple animations triggered by key presses in the game engine

I have very little Python knowledge and I'm trying to get a character to play one animation when the A key is pressed, a different animation when the D key is pressed, a different animation when the W key is pressed, and finally a different animation when both the A and D keys are pressed at the same time. I hooked up the appropriate keyboard sensors and action actuators to a python controller and when I test the character, it works with the exception of one problem. Depending on how the keys are pressed in various combinations (not entirely sure which combinations are at fault) the animation will sometimes get stuck as if Blender doesn't recognize that a key has been released and it keeps on playing that keys animation or it will stop playing the animation but will be frozen on a particular frame. I've tried numerous variations of the code, to solve the problem but nothing works and I've tried using just logic bricks without any code, but nothing eliminates the problem from occurring with certain key combinations (like pressing A and W at the same time and then releasing the W key slightly before the A key. This causes the character to get stuck on the A key's animation)

Here is the code I used:

import bge

cont=bge.logic.getCurrentController()

a=cont.sensors['a']

d=cont.sensors['d']

left=cont.actuators['turn_left']

right=cont.actuators['turn_right']

stationary=cont.actuators['stationary']

w=cont.sensors['w']

walk=cont.actuators['walk']

if a.positive and d.status==0:

cont.activate(left)

else:

cont.deactivate(left)

if d.positive and a.status==0:

cont.activate(right)

else:

cont.deactivate(right)

if a.positive and d.positive:

cont.activate(stationary)

else:

cont.deactivate(stationary)

if w.positive:

cont.activate(walk)

else:

cont.deactivate(walk)


Here is a screenshot:

• @Amir The preferred formatting for keyboard keys is to use <kbd> formatting. E.g. <kbd>A</kbd>. Oct 16 '18 at 17:02

To avoid that problem you can play the animations with python via playAction().