Calming The Hyperactive Knight


Wow, this is hilarious.
This thread should be about drinking coffee too late in the evening but it is not.

1.) I want to reply to a closed thread.
@sleepykraken asked if we could flip the character only if needed.
This was driving me crazy too so I used a simplified if/elseif rather than the new ternary operator he just taught.

//Direction.x <= 0.f ? RightLeft = -1 : RightLeft = 1.f;
if (Direction.x < 0.f)    RightLeft = -1;
else if (Direction.x > 0.f)    RightLeft = 1;

This is okay, right? I’ll keep that operator there but this seems to have fixed it.
In the event of no input, neither case triggers.

2.) Why is my dude going nuts!?!?
I tried adding dT as we did previously and I think this might be related but I’m not seeing it right now.

int main()
    bool Collision{};       // initialize collision Game State as false
    InitWindow(WindowDimensions[0], WindowDimensions[1], "Top Down Game");

    //texture variables
    Texture2D Map = LoadTexture("nature_tileset/WorldMap.png");
    Vector2 MapPos = {0.0, 0.0};
    float PlayerSpeed{250};
    Texture2D KnightIdle = LoadTexture("characters/knight_idle_spritesheet.png");
    Texture2D KnightRun = LoadTexture("characters/knight_run_spritesheet.png");
    Texture2D Knight = LoadTexture("characters/knight_idle_spritesheet.png");
    Vector2 KnightPos{
        (float)WindowDimensions[0]/2.0f - 4.0f * (0.5f *(float)Knight.width/6.0f),
        (float)WindowDimensions[1]/2.0f - 4.0f * (0.5f *(float)Knight.height)

    // 1: facing right, -1 : facing left
    float RightLeft{1.f};
    // animation variables
    float RunningTime{};
    int Frame{};
    const int MaxFrames{6};
    const int UpdateTime{1/12};

    while (!WindowShouldClose())
        // delta time (time since last frame)
        const float dT = GetFrameTime();

        // Draw Map
        DrawTextureEx(Map, MapPos, 0, 4.0, WHITE);

        // Update Animation
        RunningTime += dT;
        if (RunningTime >= UpdateTime)
            RunningTime = 0.f;
            if (Frame >= MaxFrames)    Frame = 0;
        // if game has not started
        if (!GameStarted)           
            DisplayTitleWaitForInput();      // display title and keys and wait for input      
        // if my player has won
        else if (WinGame)
            //Checkpoint = CheckpointDistance;
        // if the game is afoot
        else if (GameStarted)
            GameTimer += dT;

            // Draw Character   
            Rectangle Source    {Frame * Knight.width/6.f, 0.f, RightLeft * (float)Knight.width/6.f, (float)Knight.height};
            Rectangle Dest      {KnightPos.x, KnightPos.y, 4.0f * (float)Knight.width/6.0f, 4.0f * (float)Knight.height};
            DrawTexturePro(Knight, Source, Dest,Vector2{}, 0.f, WHITE);

            // Handle Movement
            Vector2 Direction{};
            if (IsKeyDown(KEY_A)) Direction.x -= 1.0;
            if (IsKeyDown(KEY_D)) Direction.x += 1.0;
            if (IsKeyDown(KEY_W)) Direction.y -= 1.0;
            if (IsKeyDown(KEY_S)) Direction.y += 1.0;
            if (Vector2Length(Direction) != 0.0)
                // set MapPos = MapPos - Direction            
                MapPos = Vector2Subtract(MapPos, Vector2Scale(Vector2Normalize(Direction), (PlayerSpeed * dT)));
                //Direction.x <= 0.f ? RightLeft = -1 : RightLeft = 1.f;
                if (Direction.x < 0.f)    RightLeft = -1;
                else if (Direction.x > 0.f)    RightLeft = 1;
                Knight = KnightRun;
                Knight = KnightIdle;


1 Like

This was not related to Delta Time.

Remember when we defined our update time as a float?
I made it an int.
So I guess it must have always come up as zero?
Anyway, Knight goes crazy.

Now he’s animating as expected but I kinda liked him fast. What if we wanted the run animation to be 60fps but the idle to remain at 12fps?

You could setup a separate updateTime for the running animation, then have a flag for “is running” and use that.

1 Like

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

Privacy & Terms