Jump to content
  • Advertisement
Sign in to follow this  
ninjacaleb@gmail.com

Making the sprite animation walk...

This topic is 2495 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

Advertisement
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!