Scarfy speed reduced after Nebula animation added

Hi there, after I added the Nebula (hazard) animation, Scarfy has started running slower, the way I got it to run faster was to change the const float for the Nebula to 1.0/60.0 but that should probably not be the case. Any idea what could have caused this?

#include "raylib.h"

int main()
{
    // window dimensions
    const int WindowWidth = 800; // make a Constant 'const' prevent you from changing form initial value
    const int WindowHeight = 500;
    // initialise window
    InitWindow(WindowWidth, WindowHeight, "Dapper Dasher");

    // Rectangle position (NOT WORKING)
    // int RecPosX = (WindowWidth/2);

    // is rectangle in the air
    bool isInAir = false; // initialise to false by default (will only run when false)

    // rectangle dimensions (Remove after Scarfy image has been added)
    //const int RecWidth = 40;
    //const int RecHeight = 60;

    //Rectangle Position (replaced with Scarfy Image Pos) 
    //int RecPosY = (WindowHeight - RecHeight);  //position of rectangle
    
    // Velocity
    int Velocity = 1; // DISTANCE/TIME i.e.  (pixels/frame) "-" to go upwards, take "-" away to go downwards 

    // acceleration due to gravity
    const int gravity = 1000; // (pixel/s)/s (how many pixels per second we will increase the downward velocity/second)
    //jump velocity (pixels/second) milliseconds = 1000 (1000 milliseconds in a second)

    const int jumpVelocity = -800; //minus to let object go upwards

    //Load texture from file from Sprite Sheet 
    Texture2D scarfySprite = LoadTexture("textures/scarfy.png");
   
    //Draw rectangle on Sprite Sheet
    Rectangle scarfySpriteRec;
    scarfySpriteRec.width = scarfySprite.width/6;
    scarfySpriteRec.height = scarfySprite.height;
    scarfySpriteRec.x = 0;
    scarfySpriteRec.y = 0;

    // Placement of rectangle on Sprite Sheet
    Vector2 scarfySpritePos;
    scarfySpritePos.x = WindowWidth/2 - scarfySpriteRec.width/2;
    scarfySpritePos.y = WindowHeight - scarfySpriteRec.height;
    
    // animation frames
    int frame = 0;
    const float updateTime = 1.0 / 12.0; //image get's updated 12/second (amount of time before we udpate the animation frame)
    float runningTime = 0;

    // hazard animation
    Texture2D HazardSprite = LoadTexture("textures/12_nebula_spritesheet.png");
    Rectangle HazardRec{0.0, 0.0, HazardSprite.width/8, HazardSprite.height/8};
    // HazardRec.width = HazardRec.width/8;
    // HazardRec.height = HazardRec.height/8;
    // HazardRec.x = 0;
    // HazardRec.y = 0;
    Vector2 HazardSpritePos {WindowWidth, WindowHeight - HazardRec.height};
    // HazardSpritePos.x = WindowWidth;
    // HazardSpritePos.y = WindowHeight - HazardRec.height;

    int HazardVelocity = -500; //hazard x velocity (pixels per second)

    int hazardAnimationFrame = 0;
    const float hazardAnimationUpdateTime = 1.0 / 12.0;
    float hazardAnimationRunningTime = 0;

    SetTargetFPS(60);
    while (!WindowShouldClose()) // means (WindowShouldClose() == false); "!" = negation operator
    {
        BeginDrawing();

        ClearBackground(WHITE);
        
        float dT{GetFrameTime()}; //dT = DeltaTime - time between frames/last time function was called

        // ground check
        if (scarfySpritePos.y >= WindowHeight - scarfySpriteRec.height) 
        {
        // rectangle is on the ground
        Velocity = 0;
        isInAir = false; //this is where the statement will run
        }

        else {
        // rectangle in the air - APPLY gravity FIRST before Y position as this impacts the position 
        Velocity += gravity * dT; 
        isInAir = true; // this is where the statement won't run
        //frame = 0; can do but not fluid as returns to '0' position.
        }
        // CHECK FOR JUMPING - execute a function, if "key is pressed" {this needs to happen}
        if (IsKeyPressed(KEY_SPACE) && !isInAir) // && will check for both statements to be true to return 'true'/activate. "!" negation.
                                                 // to succeed 'Key to be pressed' & NOT is in air is true ALSO CAN BE written as "isInAir == false";
        {    
        Velocity += jumpVelocity;
        }
        // update hazard sprite position
        HazardSpritePos.x += HazardVelocity * dT;

        // update Scarfy Sprite position
        scarfySpritePos.y += Velocity * dT;
        
        hazardAnimationRunningTime += dT;
        if (hazardAnimationRunningTime >= hazardAnimationUpdateTime) {
            hazardAnimationRunningTime = 0.0;

            HazardRec.x = hazardAnimationFrame * HazardRec.width;
            hazardAnimationFrame ++;

            if (hazardAnimationFrame > 7){
                HazardRec.x = 0;
                HazardRec.y ++;
            }

             if (HazardRec.y > 6 && HazardRec.x > 7) {
                 HazardRec.y = 0;
                 HazardRec.x = 0;
             }

            hazardAnimationRunningTime = 0.0;

            HazardRec.x = hazardAnimationFrame * HazardRec.width;
            HazardRec.y = hazardAnimationFrame * HazardRec.height;


            if (!isInAir)
            {
            //update running time
            runningTime += dT; // same as: runningTime = (runningTime + dT)
            
                if (runningTime >= updateTime) 
                {
                runningTime = 0.0;
                // update animation frame
                scarfySpriteRec.x = frame * scarfySpriteRec.width;
                frame ++;
                
                    if (frame > 5) 
                    {
                    frame = 0;
                    }     
                }
            
            }

        }
        //REMEMBER TO DRAW THE IMAGE to initiate it
        DrawTextureRec(HazardSprite, HazardRec, HazardSpritePos, WHITE);

        //DrawRectangle(WindowWidth/2, RecPosY, RecWidth, RecHeight, BLUE);
        DrawTextureRec(scarfySprite, scarfySpriteRec, scarfySpritePos, WHITE);
        
    EndDrawing();
    }
    UnloadTexture(scarfySprite);//important to unload the texture so that it appears in the frame
    UnloadTexture(HazardSprite);
    CloseWindow();
}```

Take a look at this section if your code, I placed a couple of comments at the important parts for emphasis

//UPDATE HAZARD ANIMATION
        hazardAnimationRunningTime += dT;
        if (hazardAnimationRunningTime >= hazardAnimationUpdateTime) {
            hazardAnimationRunningTime = 0.0;

            HazardRec.x = hazardAnimationFrame * HazardRec.width;
            hazardAnimationFrame ++;

            if (hazardAnimationFrame > 7){
                HazardRec.x = 0;
                HazardRec.y ++;
            }

             if (HazardRec.y > 6 && HazardRec.x > 7) {
                 HazardRec.y = 0;
                 HazardRec.x = 0;
             }

            hazardAnimationRunningTime = 0.0;

            HazardRec.x = hazardAnimationFrame * HazardRec.width;
            HazardRec.y = hazardAnimationFrame * HazardRec.height;

            //UPDATE SCARFY ANIMATION
            if (!isInAir)
            {
            //update running time
            runningTime += dT; // same as: runningTime = (runningTime + dT)
            
                if (runningTime >= updateTime) 
                {
                runningTime = 0.0;
                // update animation frame
                scarfySpriteRec.x = frame * scarfySpriteRec.width;
                frame ++;
                
                    if (frame > 5) 
                    {
                    frame = 0;
                    }     
                }
            
            }

        }

You’ve placed the animation code for Scarfy inside the animation code for the nebula hazards, so scarfy’s animation only updates when the nebula animation updates (and not every nebula frame).

To fix this, place the code outside of the hazard animation code like so.

//UPDATE HAZARD ANIMATION
        hazardAnimationRunningTime += dT;
        if (hazardAnimationRunningTime >= hazardAnimationUpdateTime) {
            hazardAnimationRunningTime = 0.0;

            HazardRec.x = hazardAnimationFrame * HazardRec.width;
            hazardAnimationFrame ++;

            if (hazardAnimationFrame > 7){
                HazardRec.x = 0;
                HazardRec.y ++;
            }

             if (HazardRec.y > 6 && HazardRec.x > 7) {
                 HazardRec.y = 0;
                 HazardRec.x = 0;
             }

            hazardAnimationRunningTime = 0.0;

            HazardRec.x = hazardAnimationFrame * HazardRec.width;
            HazardRec.y = hazardAnimationFrame * HazardRec.height;
        }

        //UPDATE SCARFY ANIMATION
        if (!isInAir)
        {
        //update running time
        runningTime += dT; // same as: runningTime = (runningTime + dT)
        
            if (runningTime >= updateTime) 
            {
            runningTime = 0.0;
            // update animation frame
            scarfySpriteRec.x = frame * scarfySpriteRec.width;
            frame ++;
            
                if (frame > 5) 
                {
                frame = 0;
                }     
            }
        }

I also like how you’ve attempted to get the whole nebula spritesheet working when we were only concerned with the top row. But there’s a bug in it (hint: how many sprites are on the bottom row?).

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

Privacy & Terms