Hazard(Nebula) error (C++ Fundementals course)

I don’t know why, but when I run my game I don’t see my nebula hazard moving. I tried to see my mistakes but I did it exactly like it was said in the video. Could I please get help with this?

Hello Bea8st,

I’d love to help you out. However, it helps me out greatly if you can share your code so I can see it.

I cant send an image but i can share all the code related to my nebula hazard.

// nebula variables
Texture2D nebula = LoadTexture(“texture/12_nebula_spritesheet”);
Rectangle nebRec{0.0, 0.0, nebula.width/8.0f, nebula.height/8.0f};
Vector2 nebPos{windowWidth, windowHeight - nebRec.height};

// nebula X velocity (pixels/second)
int nebVel{-200};

// nebula animation variables
int nebFrame{};
const float nebUpdateTime{1.0/12.0};
float nebRunningTime{};

//update nebula position
nebPos.x += nebVel * dT;

// update nebula animation frame
nebRunningTime += dT;
if (nebRunningTime >= nebUpdateTime)
{
nebRunningTime = 0.0;
nebRec.x = nebFrame * nebRec.width;
nebFrame++;
if (nebFrame > 7)
{
nebFrame = 0;
}
}

//draw nebula
DrawTextureRec(nebula, nebRec, nebPos, WHITE);

UnloadTexture(nebula);

Ok, I’m not seeing the immediate problem based of what you’ve shown me. Can I see all of your code so I can test it on my end?

Text works much better than a screenshot.

Ok there you go

#include “raylib.h”

int main()
{
// window dimensions
const int windowWidth{512};
const int windowHeight{380};

InitWindow(windowWidth, windowHeight, "Dapper Dasher!");

// acceleration due to gravity (pixels/frame/)/s
const int gravity{1'000};

// nebula variables
Texture2D nebula = LoadTexture("texture/12_nebula_spritesheet");
Rectangle nebRec{0.0, 0.0, nebula.width/8.0f, nebula.height/8.0f};
Vector2 nebPos{windowWidth, windowHeight - nebRec.height};

// nebula X velocity (pixels/second)
int nebVel{-200};

// nebula animation variables
int nebFrame{};
const float nebUpdateTime{1.0/12.0};
float nebRunningTime{};

// scarfy variables
Texture2D scarfy = LoadTexture("textures/scarfy.png");
Rectangle scarfyRec;
scarfyRec.width = scarfy.width/6;
scarfyRec.height = scarfy.height;
scarfyRec.x = 0;
scarfyRec.y = 0;
Vector2 scarfyPos;
scarfyPos.x = windowWidth/2 - scarfyRec.width/2;
scarfyPos.y = windowHeight - scarfyRec.height;

// animation frame
int frame{};
// amount of time before we update the animation frame
const float updateTime{1.0/12.0};
float runningTime{}; 

// is the rectangle in the air?
bool isInAir{};
 // jump velocity(pixels/second)
const int jumpVel{-600};

int velocity{0};

SetTargetFPS(60);
while (!WindowShouldClose())
{
    // delta time (time since last frame)

    const float dT{GetFrameTime()};   

    // start drawing
    BeginDrawing();
    ClearBackground(WHITE);

    // ground check
    if (scarfyPos.y >= windowHeight - scarfyRec.height)
    {
       // rectangle is on the ground
       velocity = 0;
       isInAir = false;
    }
    else
    {
        // rectangle is in the air
         velocity += gravity * dT;
         isInAir = true;
    }
    // check for jumping
    if (IsKeyPressed(KEY_SPACE) && !isInAir)
    {
        velocity += jumpVel;
    }

   //update nebula position
   nebPos.x += nebVel * dT;

    // update scarfy position
    scarfyPos.y += velocity * dT;

   // update scarfy's animation frame
   if(!isInAir)
   {
        // update running time
    runningTime += dT;
    if (runningTime >= updateTime)
    {
        runningTime = 0.0;
          // update animation frame
    scarfyRec.x = frame * scarfyRec.width;
    frame++;
    if (frame > 5)
    {
        frame = 0;
       }
     }
   }   

    // update nebula animation frame
    nebRunningTime += dT;
    if (nebRunningTime >= nebUpdateTime)
    {
        nebRunningTime = 0.0;
        nebRec.x = nebFrame * nebRec.width;
        nebFrame++;
        if (nebFrame > 7)
        {
            nebFrame = 0;
        }
    }       

    //draw nebula
    DrawTextureRec(nebula, nebRec, nebPos, WHITE);

    // draw scarfy
    DrawTextureRec(scarfy, scarfyRec, scarfyPos, WHITE);

    // stop drawing
    EndDrawing();
}
UnloadTexture(scarfy);
UnloadTexture(nebula);
CloseWindow();

}

Awesome, that’s exactly what I needed!

And here’s the problem, take a look at this line
Texture2D nebula = LoadTexture("texture/12_nebula_spritesheet");

There’s two issues here, both with the file path:

  1. Judging by the way you setup scarfy, texture should be textures in the file path

  2. You forgot the file extension at the end of the filename, 12_nebula_spritesheet should be 12_nebula_spritesheet.png

Fix those, and everything else is fine. In hindsight I could have almost spotted that with your first example but this way I’m also able to spot other issues if they were present.

Thank you so much for the help!

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

Privacy & Terms