Making the sprite animation walk...

Started by
2 comments, last by BeerNutts 12 years, 2 months ago
Well here is the deal, the sprite can move in the sense of a new x,y coordinates, but how do I make the sprite appear walking when it is moving? Sorry if I' m not being clear enough.

Thx in advance..>
Advertisement
look into the use of sprite sheets. Given a sprite sheet, it is just a matter of looping through the required frames.
Co-founder/Lead Programmer
Bonafide Software, L.L.C.
Fairmont, WV 26554 US
Thx, I began searching(Google) for tutorials,for i know absolutely nothing about it could you please post a link to a tutorial.
You don't have to use sprite sheets. And, just saying "sprite sheets" really doesn't help in explaining how you would make a character walk.

Here's what I would suggest. Let's say you have 6 sprites for the walking man:
standingleft.jpg (your man is standing, facing to the left. This will be used as the middle sprite when walking, and when not moving).
walkingleft1.jpg (your man is facing left, with right foot in front)
walkingleft2.jpg (your man is facing left, with left foot in front)
(same as left, except man is facing right)
standingright.jpg
walkingright1.jpg
walkingright2.jpg

(be advised, you could use sprite sheets here, but I'll assume you have 6 separate sprites)

You want your man, when you standing still and facing right, to show standingright.jpg, and standingleft.jpg when facing left.
When he's walking right, you'll want to rotate through the walkingright1.jpg, standingright.jpg, walkingright2.jpg, and back to standingright.jpg and repeat at a constant rate. Same with facing left, using *left sprites.

Here's how i'd do it:

// these enums dictate which animations are where
enum
{
eWalkingOne = 0,
eStanding, // eStanding is the sprite of the man with both legs under him
eWalkingTwo,
eSecondStanding, // Used to rotate back to standing easily, uses same standing sprite as eStanding
eMaxWalkingState
};

// Sprite is the container used for displaying Sprite, for whatever API you use
Sprite WalkingRight[eMaxWalkingState];
Sprite WalkingLeft[eMaxWalkingState];

// Somewhere in your main code, you need to load the Sprites:
void LoadWalkingSprites(void)
{
WalkingRight[eWalkingOne] = LoadSprite("walkingright1.jpg");
WalkingRight[eStanding] = LoadSprite("standingright.jpg");
WalkingRight[eWalkingTwo] = LoadSprite("walkingright2.jpg");
WalkingRight[eSecondStanding] = LoadSprite("standingright.jpg");
// repeat for all left sprites
}

// In the class/struct you use to hold your Hero Man, you need to add these elements
class HeroMan
{
...
unsigned int TimeLastUpdate; // holds millseconds of when we last moved the legs
bool IsWalking;
bool IsFacingRight;
int CurrentWalkState;
double XVelocity;
double YVelocity;
...
};

// you'll also need a time in millisecond you want the legs to stay the same spot
#define WALK_ANIMATION_RATE 750 // 750 milliseconds, adjust to your game

// you should also have a velocity your man travels. I'll assume it's in pixels/second
#define WALK_VELOCITY 80

// In your code that handles your input, you need to do this
void HeroMan::CheckInput(void)
{
// Check if the MoveLeft key is down, and that we aren't already moving left
if (KeyDown(MoveLeft) && (!IsWalking || IsFacingRight)) {
// Our hero is walking left. Set up him facing left and walking.
// set the TimeLastUpdate to now
TimeLastUpdate = GetTickCount(); // or use whatever your platform has to get milliseconds tick count
IsWalking = true;
IsFacingRight = false;
CurrentWalkState = eWalkingOne;
Velocity = -WALK_VELOCITY; // we want his velocity to be negative
}
// Do the same for right
}

// In your Man update Function, check the states, move player and draw the correct sprite
void HeroMan::Update(double TimeSinceLastUpdate)
{
Sprite &SpriteToDraw;

// check if we're not walking
if (!IsWalking) {
// draw the sprite, depending on which way we're facing
if (IsFacingRight) {
SpriteToDraw = WalkingRight[eStanding];
}
else {
SpriteToDraw = WalkingLeft[eStanding];
}
}
else {
// we're walking some direction. 1st check if we need to update
if (TimeLastUpdate + WALK_ANIMATION_RATE <= GetTickCount) {
// Move to next walking state
// this will cycle through eWalkingOne (right foot in front), eStanding (both feet underhim), eWlakingTwo (Left foot in front), and eSecondStanding (back to both feet under him)
CurrentWalkState++;
if (CurrentWalkState = eMaxWalkingState) {
CurrentWalkState = eWalkingOne ;
}
}
// Move player and Set the sprite to correct sprite
if (IsFacingRight) {
SpriteToDraw = WalkingRight[CurrentWalkState];
}
else {
SpriteToDraw = WalkingLeft[CurrentWalkState];
}
}
// Move the player based on his current velocity
XLocation += XVelocity * TimeSinceLastUpdate;
YLocation += YVelocity * TimeSinceLastUpdate;

// Finally, Draw the sprite now
SpriteToDraw.Draw(XLocation, YLocation);
}


That's probably a lot to swallow, but, hopefully it gives you an idea.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement