Issues With Swapping Between Animations and Frame Rate

Fair warning, what I did was a deviation from what was done in the course. Instead of freezing one animation for jumping, I decided to make a couple of jump animations myself for my character.
That said, I don’t have any idea why this is happening. The attached videos below show the problem.
Here’s my code for handling jumps (including jump input):

        //jump check
        if(IsKeyPressed(KEY_SPACE) && !isInAir)
        {
            velocity += JUMP_VELOCITY;

            int random = GetRandomValue(0, 3);
            if(random < 3)
            {
                currentLunaAnimation = lunaJump1;
                selectedJumpAnim = lunaJump1AnimData;
            }
            else
            {
                currentLunaAnimation = lunaJump2;
                selectedJumpAnim = lunaJump2AnimData;
            }
            currentRec = selectedJumpAnim.rec;
            jumpAnimPlaying = true;
        }

Then skipping code for handling velocity

        //update luna animation frame
        //Jump Animation
        if(jumpAnimPlaying)
        {
            selectedJumpAnim.runningTime += DELTA_TIME;
            if(selectedJumpAnim.runningTime >= selectedJumpAnim.updateTime)
            {
                if(velocity < 0)
                {
                    if(selectedJumpAnim.frame < 3)
                    {
                        selectedJumpAnim.frame++;
                    }
                }
                else
                {
                    if(!landing)
                    {
                        if(selectedJumpAnim.frame < 6)
                        {
                            selectedJumpAnim.frame++;
                        }
                        else if(selectedJumpAnim.frame == 6)
                        {
                            lunaAnimData.runningTime = 0;
                            landing = true;
                        }
                    }
                    else
                    {
                        lunaAnimData.frame = 7;
                        selectedJumpAnim.frame = 0;
                    }
                }
                selectedJumpAnim.runningTime = 0;
            }
            
            currentRec.x = selectedJumpAnim.frame * selectedJumpAnim.rec.width;
        }
        //Skate animation
        else
        {
            lunaAnimData.runningTime += DELTA_TIME;
            currentLunaAnimation = luna;
            if(lunaAnimData.runningTime >= lunaAnimData.updateTime)
            {
                currentRec = lunaAnimData.rec;
                lunaAnimData.frame++;
                if(lunaAnimData.frame > 17)
                {
                    lunaAnimData.frame = 0;
                }
                lunaAnimData.runningTime = 0;
                currentRec.x = lunaAnimData.frame * lunaAnimData.rec.width;
            }
        }

Here’s a video showing the problem: https://www.youtube.com/watch?v=N96_6y1qoB0

Is it accurate to assume that the issue is your player not showing up until after you jump? In case you end up forgetting to show me code that is actually relevant, can you upload your project using this form? That’ll allow me to take a closer look.

Not quite, though that is an issue I want to address at some point soon. That seems to happen randomly, sometimes I go to debug and the character is drawn just fine, other times it doesn’t.

The issue I am interested in here is the frame the run/skate animation starts on after completing the jump.
In the animation code, I set the running animation’s starting frame to ‘7’, this is in one of the nested else statements. In the skating animation, this is the same pose as the last frame of both jumping animations, so the transition between the two would be smooth.
The problem is a bit weird to me. When testing, if I set the target fps to 20 or lower, it completes this transition just fine, starting the skate at frame 7. 30 or higher, and the running animation rather than starting on frame 7, seems to start on frame 5, two before that, making the transition between jumping and running look jerky.

I’ll go ahead and upload my project using the form.

I should also clarify one thing, starting on frame 7 is technically incorrect as it increments it before it swaps frames, so it actually starts on frame 8, which is the correct frame.

Ok, to solve Luna not showing up initially this is all I did to your code, moving the initialization of currentRec and currentLunaAnimation outside of the while loop and giving them initial values based on the skating animation.

As for the 20/60 fps quirk with the animation. What’s happening at 60 fps is that when you land, there are a couple of frames where the animation is stuck on frame 7 because runningTime for the animation hasn’t triggered the next frame yet but will very quickly, making the animation look jarring. But once you go down to 20 frames per second, then deltaTime is large enough that runningTime immediately triggers the next frame.

To remedy this, I added a line that sets the runningTime equal to the updateTime like so:

This allows the animation to be set to what you are intending regardless of frame rate, but the next frame will always be triggered.

Give these changes a shot on your end and let me know what you think!

1 Like

You figured that out fast, that’s awesome! I took note of the changes in the comments so hopefully I remember in case something similar happens in the future. Thanks for the help!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms