# I just moved everything to a sprite sheet...

This topic is 2394 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So i thought it would be a good idea to move my running animation sprites to a sprite sheet to save memory, but the animation is no longer working. *Note that clips were set correctly and images were loaded in fine. I think the problem is with my enum.

enumerations.
 enum Frame{ STANDING = 0, RUNNING1 = 1, RUNNING2 = 2, RUNNING3 = 3, RUNNING4 = 4, JUMPING = 5 }; inline Frame& operator++(Frame& a) { switch(a) { case RUNNING1: a = RUNNING2; break; case RUNNING2: a = RUNNING3; break; case RUNNING3: a = RUNNING4; break; case RUNNING4: a = RUNNING1; break; } return a; }

in player.cpp
 void Player::HandleEvents() { ... if( input.KeysHeld[ SDLK_d ] == true ) { direction = RIGHT; ++frame; xVel += 5; } else if( input.KeysHeld[ SDLK_a ] == true ) { direction = LEFT; ++frame; xVel -= 5; } } 

in game.cpp
 void Game::DrawPlayer() { // Draw player if( player->direction == RIGHT ) { if( player->jumping ) graphics->ApplySurface( player->xPos, player->yPos, player->playerRight, graphics->screen, player->playerRightClips[ player->frame] ); else graphics->ApplySurface( player->xPos, player->yPos, player->playerRight, graphics->screen, player->playerRightClips[ player->frame] ); } else if( player->direction == LEFT ) { if( player->jumping ) graphics->ApplySurface( player->xPos, player->yPos, player->playerLeft, graphics->screen, player->playerLeftClips[ player->frame] ); else graphics->ApplySurface( player->xPos, player->yPos, player->playerLeft, graphics->screen, player->playerLeftClips[ player->frame] ); } } 

##### Share on other sites
You implementation of operator++ is not correct. operator++ is supposed to take a reference as argument so that you can update it and also return the same argument by reference. You also need to put break statements in your switch.inline Frame& operator++(Frame& a) { switch(a) { case RUNNING1: a = RUNNING2; break; case RUNNING2: a = RUNNING3; break; case RUNNING3: a = RUNNING4; break; case RUNNING4: a = RUNNING1; break; } return a; }

You know that enum Frame{ STANDING = 0, RUNNING1 = 1, RUNNING2 = 2, RUNNING3 = 3, RUNNING4 = 4, JUMPING = 5 }; is exactly the same as enum Frame{ STANDING, RUNNING1, RUNNING2, RUNNING3, RUNNING4, JUMPING }; Maybe you want to be explicit about the numbers. Nothing wrong with that I guess.

##### Share on other sites
okay i changed the operator++ function but its still not working

##### Share on other sites
Is HandleEvents() called every time you receive an event? You should not update the game each time you receive an event. Updating the velocity and updating the Frame should happen once every frame and not when you receive events. If HandleEvents() is called once and only once every frame the name HandleEvents is misleading.

##### Share on other sites
by frame i mean animation frame. Not the whole screen. the whole screen is updated every frame. I want the animation frame to change depending on whether buttons are held down and stuff. I had it working before i put them all on a sprite sheet.

##### Share on other sites
Ok, so HandleEvents() is called only once every frame (I mean once for for every whole screen update)? No more no less?

##### Share on other sites
Im pretty sure lol

##### Share on other sites
ok, just making sure.

I have noticed that you create a lot of threads about every single problem you have. I think many of these problems could have been solved by yourself if you knew how to debug. Many people talk about debuggers, and yes they are great. I use gdb and it has helped me a few times but most of my debugging consists of simple text output using std::cout. I find this way simpler in many cases and I think it is especially easier for beginners. To make sure that a function is being called, print something from inside the function and you can easily see that the function is being called, or not. It is also very useful to print variables at different location in the code to see that the variables has the correct values. When you have noticed that something is not right you can go on and investigate that code in more detail until you find the cause of the problem. In the code above. Look at the value of frame. Has it changed between the screen updates? If it hasn't then something is probably wrong with how you update the frame variable. You can then test if operator++(Frame&) is being called and that it works as it should. On the other hand if you find that the frame variable is being updated correctly then something is probably wrong with how you use the frame variable or maybe you use the wrong frame variable.

##### Share on other sites
here is a samle on it
its what im using, and works great.

##### Share on other sites
Alright i got that animation working but when looking left the gun doesnt show. I know you guys cant help me but i just felt like sharing lol

##### Share on other sites
It is possible that you are culling it.