Sign in to follow this  

I just moved everything to a sprite sheet...

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

If you intended to correct an error in the post then please contact us.

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.
[code]

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;
}[/code]

in player.cpp
[code]
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;
}
}
[/code]

in game.cpp
[code]
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] );
}
}
[/code]

Share this post


Link to post
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.[code]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;
}[/code]


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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this