Can't get Scarfy to animate? Locked at frame 0

As far as I can tell, the code should work, but Scarfy stays at frame 0 instead of cycling through his run animation. I could use another set of eyes if someone feels like checking over my code? I know I missed something. I’m just not sure what :slight_smile:

Thanks in advance!

Code below, or if you prefer GitHub:

https://github.com/ScaerieTale/Dapper-Dasher/dasher.cpp

#include "raylib.h"

int main()
{
    // Initial window setup
    const int window_width{512};
    const int window_height{380};
    InitWindow(window_width, window_height, "Dapper Dasher");
    SetTargetFPS(60);

    // Acceleration (pixels/sec)/sec
    const int gravity{1000};

    // jump velocity in pixles/second
    const int jumpVel{600};
    bool isInAir{false};    
    // Velocity is 0 when not jumping
    int velocity{0};
    
    // Character
    Texture2D scarfy = LoadTexture("textures/scarfy.png");
    Rectangle scarfyRec;
    scarfyRec.height = scarfy.height;
    scarfyRec.width = scarfy.width/6;
    scarfyRec.x = 0;
    scarfyRec.y = 0;
    Vector2 scarfyPos;
    scarfyPos.x = window_width/2 - scarfyRec.width/2;
    scarfyPos.y = window_height - scarfyRec.height;

    // Animation frame
    int frame{};

    // Initiate game loop
    while(!WindowShouldClose())
    {
        // In-game window setup
        BeginDrawing();
        ClearBackground(WHITE);

        // Game logic begin
       
       // Update animation frame
       scarfyRec.x = frame * scarfy.width;
       frame++;
       if (frame > 5)
       {
           frame = 0;
       }

        DrawTextureRec(scarfy, scarfyRec, scarfyPos, WHITE);

        // Physics
        // Delta Time
        const float dT{GetFrameTime()};

        // Check for ground
        if (scarfyPos.y >= window_height - scarfy.height)
        {
            // On the ground
            velocity = 0;
            isInAir = false;
        }
        else
        {
            // In mid-air, apply gravity
            velocity -= gravity * dT;
        }
        // Spacebar to jump!
        if (IsKeyPressed(KEY_SPACE) && !isInAir)
        {
            velocity += jumpVel;
            isInAir = true;
        }

        // Update position
        scarfyPos.y -= velocity * dT;



        // Game logic end
        EndDrawing();
    };
    UnloadTexture(scarfy);
    CloseWindow();
}

I couldn’t find a way to edit the post. I mistyped the github link. Sorry about that!

Correct link is https://github.com/ScaerieTale/Dapper-Dasher/blob/master/dasher.cpp

scarfyRec.x = frame * scarfy.width; should be scarfyRec.x = frame * scarfyRec.width;

Your animation is going to play REALLY fast and for now that’s ok, you’ll learn how to dial it down soon.

1 Like

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

Privacy & Terms