Problem with running animation

Aug 1, 2011 at 4:18pm
Hello, im making a survival side scroller kind of game, and im having trouble making the running animation.

Right now, the direction changes are working( if i press left he looks left, if i press right he looks right ) and when i press a direction the first frame of the running shows.

here is the relevant code

In player.cpp HandleEvents
1
2
3
4
5
6
7
8
9
10
11
12
13
14
case SDLK_d:
        direction = RIGHT;
	frame++;

	xVel += 10;
	break;
...

//loop the animation
if( frame >= 3 )
{
	frame = 0;
}


in Game.cpp Render function
1
2
3
4
5
6
7
8
9
	// Draw the character in the right direction and frame
	if( player->direction == RIGHT )
	{
		graphics->ApplySurface( player->xPos, player->yPos, player->rightAnimation[player->frame], graphics->screen );
	}
	else if( player->direction == LEFT )
	{
		graphics->ApplySurface( player->xPos, player->yPos, player->leftAnimation[player->frame], graphics->screen );
	}
Aug 1, 2011 at 4:34pm
Firstly, what is your problem? It's kind of crucial that you mention that. To solve the problem we might also need to know how your sprites are laid out. Does one row correspond to one full animation sequence and does each animation have the same number of frames for example.

You're also storing animation data in your player which seems a little odd to me, I always try to seperate the graphics component from my physical entities as much as possible.
Last edited on Aug 1, 2011 at 4:35pm
Aug 1, 2011 at 4:53pm
Well, im really just using this game to gain experience making games( this will be my first) and to build a game engine at the same time.

The problem is that when i press right button, instead of the animation going.. frame1, frame2, it just goes to frame1 and thats it.

I dont know what you mean by rows, but each sprite is its own separate image. I know i should use sprite sheets but i diddnt feel like it. In my next project I'll make sure to though.
Aug 1, 2011 at 4:59pm
you handle a key down event and increase the frame number when D is pressed, but if you HOLD a key down that doesn't produce a key down event every frame, only one key down event occurs when the key is first pressed, and a key up event when it is released. You'll have to come up with a mechanism of storing whether the key is being held and increasing the frame number every frame if it is.
Last edited on Aug 1, 2011 at 5:01pm
Aug 1, 2011 at 5:06pm
Alright thanks I'll work on that and come back if i'm still having problems
Aug 1, 2011 at 5:23pm
I know this is going to sound like im not even trying but could you at least give me some psuedocode to help me?
Last edited on Aug 1, 2011 at 5:28pm
Topic archived. No new replies allowed.