Multiple Nebula Hazards

Here is my code:
#include “raylib.h”

int main() {

const int screenWidth{800};

const int screenHeight{600};

//FPS

SetTargetFPS(60);

//CHARACTER MAIN

int posX {screenWidth/2};

float velocity {0};

bool isJumping {};

const float jumpForce {-2000};

InitWindow(screenWidth, screenHeight, “Runner”);

//GRAVITY

const float gravity {20};

//LOAD TEXTURES && VARIABLES FOR CHARACTER

Texture2D Runner = LoadTexture(“textures/Runner.png”);

Rectangle RunnerRec ;

RunnerRec.width = Runner.width/6;

RunnerRec.height = Runner.height;

RunnerRec.x = 0;

RunnerRec.y = 0;

Vector2 RunnerPosition;

RunnerPosition.x = screenWidth/2 - RunnerRec.width/2;

RunnerPosition.y = screenHeight - RunnerRec.height;

//ANIM FRAME

int frame{};

//RUNNING

const float updateTime {1.0 / 12.0};

float runningTime {0};

//HAZARD TEXTURES

Texture2D Nebula = LoadTexture(“textures/12_nebula_spritesheet.png”);

Rectangle NebRec {0.0 , 0.0 , Nebula.width/8.f , Nebula.height/8.f};

Vector2 nebPos {screenWidth + 50, screenHeight - NebRec.height};

//2ND NEBULA

Rectangle Neb2Rec{0.0 , 0.0 , Nebula.width/8.f , Nebula.height/8.f};

Vector2 neb2Pos {screenWidth + 250, screenHeight - NebRec.height};

//NEBULA X VELOCITY

int NebVel {-200};

//NEBULA VARIABLES

int nebFrame;

const float nebUpdateTime (1.0/12.0);

float nebRunnginTime;

//NEBULA 2 VARIABLES

int neb2Frame;

const float neb2UpdateTime {1.0/16.0};

float neb2RunningTime;

//WHILE LOOP

while (!WindowShouldClose())

{

BeginDrawing ();

ClearBackground(WHITE);

float dT{GetFrameTime()};

//Update Position

RunnerPosition.y += velocity;

//CHECK IF CHAR IS OUT OF SCREEN

if (posX + Runner.width > screenWidth)

{

posX = screenWidth - Runner.width;

}

if (RunnerPosition.y + RunnerRec.height > screenHeight)

{

RunnerPosition.y = screenHeight - RunnerRec.height;

}

if (RunnerPosition.y < 0)

{

RunnerPosition.y = 0;

velocity = 0;

}

//JUMP MECHANICS

if (RunnerPosition.y >= screenHeight - RunnerRec.height)

{

    velocity = 0;

    isJumping = false;

}

else

{

velocity += gravity * dT;

isJumping = true;

}

if (IsMouseButtonPressed (MOUSE_BUTTON_LEFT) && !isJumping)

{

    velocity += jumpForce * 0.25f * dT;

}

// UPDATE NEBULA POSITION

nebPos.x += NebVel * dT ;

//UPDATE 2ND NEBULA POSITION

neb2Pos.x += NebVel * dT;

//UPDATE RUNNER POSITION

RunnerPosition.y += velocity;

// RUNNER ANIM FRAME

if (!isJumping)

{

    //UPDATE RUNNING TIME

    runningTime += dT;

    if (runningTime >= updateTime)

    {

        runningTime = 0;

       

    //UPDATE ANIM FRAME

        RunnerRec.x = frame * RunnerRec.width;

        frame++;

        if (frame > 5)

        {

            frame = 0;

        }

    }



}



//NEBULA ANIM FRAME

nebRunnginTime += dT;

if (nebRunnginTime >= nebUpdateTime)

{

    nebRunnginTime = 0;

    NebRec.x = nebFrame * NebRec.width;

    nebFrame++ ;

    if (nebFrame > 7)

    {

        nebFrame = 0;

    }

   

}

  //NEBULA 2 ANIM FRAME

neb2RunningTime += dT;

if (neb2RunningTime >= neb2UpdateTime)

{

    neb2RunningTime = 0;

    Neb2Rec.x = neb2Frame * Neb2Rec.width;

    neb2Frame++ ;

    if (neb2Frame > 7)

    {

        neb2Frame = 0;

    }

   

}




//DRAW HAZARD

DrawTextureRec(Nebula, NebRec, nebPos, WHITE);

//DRAW 2ND HAZARD

DrawTextureRec (Nebula, Neb2Rec, neb2Pos, WHITE);

//DRAW RUNNER

DrawTextureRec(Runner, RunnerRec, RunnerPosition, WHITE);

EndDrawing();

}

UnloadTexture (Runner);

UnloadTexture (Nebula);

CloseWindow();

}
For some reason the Nebulas sometimes appear, sometimes one appears, sometimes just one appears, how can I fix this?

Don’t forget to initialize your variables, nebFrame and neb2Frame were declared but not initialized, so the compiler would sometimes give them junk data. This would balance itself out if the junk value is about 7, but sometimes it can be well below 0.

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

Privacy & Terms