Animations do not play until joystick analog event ends

Hello, I have recently been working with adding animations into JME. I am using an analog listener to detect gamepad input to move my character. The mapping and listening work well. However, I have an issue with the animation: the animation will not play until the gamepad joysticks are released.

The code I am using in the update loop is:

   if(upL1 && upR1)
   {
       dir = new Vector3f(upL1Val, 0, upR1Val);
       channel.setAnim("Forwards", 0.50f);
       System.out.println(channel.getAnimationName());
       channel.setLoopMode(LoopMode.Loop);
       animStartTime = System.currentTimeMillis();
   }
   else if(downL1 && downR1)
   {
       dir = new Vector3f(-downL1Val, 0, -downR1Val);
       channel.setAnim("Backwards", 0.50f);
       channel.setLoopMode(LoopMode.Loop);
       animStartTime = System.currentTimeMillis();
   }
   else if(upL1 && downR1)
   {
       rot = -(upL1Val + downR1Val);
       channel.setAnim("Left", 0.50f);
       channel.setLoopMode(LoopMode.Loop);
       animStartTime = System.currentTimeMillis();
   }
   else if(downL1 && upR1)
   {
       rot = downL1Val + upR1Val;
       channel.setAnim("Right", 0.50f);
       channel.setLoopMode(LoopMode.Loop);
       animStartTime = System.currentTimeMillis();
   }

The Boolean conditions (which I set to true when input is found) all run as expected. Note that the System.out.println(channel.getAnimationName()); in the first of the if statements, prints out “Forwards” whenever input from the joysticks in the forward direction is detected. The problem is that the animation does not begin until the joysticks are released, even though the channel thinks it is playing.

How can I make the animation begin to play as soon as input is detected?

Thank you very much!
–Artur Hawkwing

Analog listener gives a continuous update of the input values as long as input is detected. If you’re just setting the flags when the listener is triggered and then the code above is run with each update, then you will have the effect of continually re-starting the animations over and over again with each update until the input ends, which would give the appearance that your models are not animating until you release your joysticks since the animations will be restarted before they can even begin to play.

2 Likes
1 Like

Thank you both! I added in the code to prevent repeated activation, and it works perfectly now.
For anyone else who wants to see the solution, I did:

if(upL1 && upR1)
       {
           dir = new Vector3f(upL1Val, 0, upR1Val);
           animStartTime = System.currentTimeMillis();
           if(!channel.getAnimationName().equals("Forwards"))
           {
               channel.setAnim("Forwards", 0.50f);
               channel.setLoopMode(LoopMode.Loop);
           }
       }
       else if(downL1 && downR1)
       {
           dir = new Vector3f(-downL1Val, 0, -downR1Val);
           if(!channel.getAnimationName().equals("Backwards"))
           {
               channel.setAnim("Backwards", 0.50f);
               channel.setLoopMode(LoopMode.Loop);
               animStartTime = System.currentTimeMillis();
           }
       }
       else if(upL1 && downR1)
       {
           rot = -(upL1Val + downR1Val);
           if(!channel.getAnimationName().equals("Left"))
           {
               channel.setAnim("Left", 0.50f);
               channel.setLoopMode(LoopMode.Loop);
               animStartTime = System.currentTimeMillis();
           }
       }
       else if(downL1 && upR1)
       {
           rot = downL1Val + upR1Val;
           if(!channel.getAnimationName().equals("Right"))
           {
               channel.setAnim("Right", 0.50f);
               channel.setLoopMode(LoopMode.Loop);
               animStartTime = System.currentTimeMillis();
           }

Cool beans. Glad you got it working. :slight_smile: