Update Animation Data

I cant get Scarfy to animate properly, can you point out where I am wrong?:

#include “raylib.h”

struct AnimData

{

Rectangle rec;

Vector2 pos;

int frame;

float updateTime;

float runningTime;

};

bool isOnGround(AnimData data, int windowHeight)

{

return data.pos.y >= windowHeight - data.rec.height;

}

AnimData updateAnimData(AnimData data, float deltaTime, int maxFrame)

{

//update running time

data.runningTime += deltaTime;

if (data.runningTime >= data.updateTime)

{

    data.runningTime = 0.0;

    //update anim frame

    data.rec.x = data.frame * data.rec.width;

    data.frame++;

    if (data.frame > maxFrame)

    {

        data.frame = 0;

    }  

}

return data;

}

int main()

{

int windowD [2];

windowD[0] = 800;

windowD[1] = 450;

int frame {0};

float updateTime {1.0 / 12.0};

float runningTime {};

bool IsInAir {false};

const int jumpPower {600};

InitWindow (windowD[0],windowD[1], "Dapper-Dasher");

const int gravity {1500};

int veloc {0};

//nebula vars

Texture2D neb = LoadTexture("textures/12_nebula_spritesheet.png");

/*

AnimData nebD

    {{0.0, 0.0, neb.width / 8, neb.height / 8},

    {windowD[0], windowD[1] - neb.height / 8},

    0,

    1.0 / 12.0,

    0};

AnimData nebD2

    {{0.0, 0.0, neb.width / 8, neb.height / 8},

    {windowD[0] + 320, windowD[1] - neb.height / 8},

    0,

    1.0 / 16.0,

    0.0};

*/

    const int numOfNeb {20};

    AnimData nebulae[numOfNeb] {};

    for (int i = 0; i < numOfNeb; i++)

    {

        nebulae[i].rec.x = 0.0;

        nebulae[i].rec.y = 0.0;

        nebulae[i].rec.width = neb.width / 8;

        nebulae[i].rec.height = neb.height / 8;

        nebulae[i].pos.y = windowD[1] - neb.height/8;

        nebulae[i].frame = 0;

        nebulae[i].runningTime = 0.0;

        nebulae[i].updateTime = 0;

        nebulae[i].pos.x = windowD[0] + i * 450;

    }

/*

Rectangle nebRect {0.0, 0.0, neb.width / 8, neb.height / 8};

Vector2 nebPos {wid, higt - nebRect.height};



int nebFrame {};

const float nebUpdateTime {1.0 / 12.0};

float nebRunTime {};

*/

int nebV {-350};

//scarfy vars

AnimData scarf;

Texture2D scarfy = LoadTexture("textures/scarfy.png");

scarf.rec.width = scarfy.width/6;

scarf.rec.height = scarfy.height;

scarf.rec.x = 0;

scarf.rec.y = 0;

scarf.pos.x = (windowD[0]/2) - (scarf.rec.width/2);

scarf.pos.y = (windowD[1] - scarf.rec.height);

scarf.frame = 0;

scarf.updateTime = (1.0/12);

scarf.runningTime = 0.0;

/*

Rectangle scarfyRect;

Vector2 scarfyPos;

scarfyRect.width = scarfy.width / 6;

scarfyRect.height = scarfy.height;

scarfyRect.x = 0;

scarfyRect.y = 0;

scarfyPos.x = wid / 2 - scarfyRect.width + (scarfyRect.width / 2);

scarfyPos.y = higt - scarfyRect.height;

scarf.frame = 0;

scarf.updateTime = 1.0 / 12.0;

scarf.runningTime = 0.0;

*/

SetTargetFPS (60);

while (!WindowShouldClose())

{

    BeginDrawing();

    const float dt{GetFrameTime()};

    // background color

    ClearBackground(WHITE);

   

    scarf.pos.y += veloc * dt;

   

    for (int i = 0; i < numOfNeb; i++)

    {

        nebulae[i].runningTime += dt;

        if (nebulae[i].runningTime >= nebulae[i].updateTime) {

            nebulae[i].runningTime = 0.0;

            nebulae[i].rec.x = nebulae[i].frame * nebulae[i].rec.width;

            nebulae[i].frame ++;

            if (nebulae[i].frame > 7) {

                nebulae[i].frame = 0;

            }

        }

    }

   

    /*

    nebulae[0].runningTime += dt;

    if (nebulae[0].runningTime >= nebulae[0].updateTime) {

        nebulae[0].runningTime = 0.0;

        nebulae[0].rec.x = nebulae[0].frame * nebulae[0].rec.width;

        nebulae[0].frame ++;

        if (nebulae[0].frame > 7) {

            nebulae[0].frame = 0;

        }

    }

    nebulae[1].runningTime += dt;

    if (nebulae[1].runningTime >= nebulae[1].updateTime) {

        nebulae[1].runningTime = 0.0;

        nebulae[1].rec.x = nebulae[1].frame * nebulae[1].rec.width;

        nebulae[1].frame ++;

        if (nebulae[1].frame > 7) {

            nebulae[1].frame = 0;

        }

    }

    */

     

    if (isOnGround(scarf, windowD[1]))

    {

        veloc = 0;

        scarf.pos.y = windowD[1] - scarf.rec.height;

        IsInAir = false;

    } else {

        veloc +=   gravity * dt;

        IsInAir = true;

    }

   

    if (IsKeyPressed(KEY_SPACE) && !IsInAir)

    {

        veloc -= jumpPower;

    }

   

    for (int i = 0; i < numOfNeb; i++)

    {

        nebulae[i].pos.x += nebV * dt;

    }

   

    /*

    nebulae[0].pos.x += nebV * dt;

    nebulae[1].pos.x += nebV * dt;

    */

    scarf.rec.x = frame * scarf.rec.width;

    if (!IsInAir)

    {

       scarf = updateAnimData(scarf, dt, 5);

    }

    else

    {

        scarf.rec.x = scarf.frame * scarf.rec.width;

    }

    for (int i = 0; i < numOfNeb; i++)

    {

        DrawTextureRec(neb, nebulae[i].rec, nebulae[i].pos, PURPLE);

    }

   

    /*

    DrawTextureRec(neb, nebulae[0].rec, nebulae[0].pos, PURPLE);

    DrawTextureRec(neb, nebulae[1].rec, nebulae[1].pos, PINK);

    */

    DrawTextureRec(scarfy, scarf.rec, scarf.pos, WHITE);

    EndDrawing();

}

UnloadTexture(scarfy);

UnloadTexture(neb);

CloseWindow();

}

Hi Stew,

You have this line just above if(!IsInAir)

scarf.rec.x = frame * scarf.rec.width;

Which is resetting the position of scarf.rec.x to 0 as frame = 0 and is otherwise not being increased or being used. This seems like code left over after moving to the use of AnimData.

1 Like

thanks, I just commented it out and it worked!

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

Privacy & Terms