Jump to content
  • Advertisement
Sign in to follow this  

Simple 2D animation

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

Hi, I'm thinking of adding animation to my pacman. So what I have is a series of files. Pacman01.tga Pacman02.tga Pacman03.tga Pacman04.tga I'd like to scroll through these four animations without having to type a large switch statement. I don't want to use switch( AnimationNumber % 4) { case 0: //load pacman01 break; case 1: //load pacman02 ... ... } because what if I had 99 different animations and I needed to type them all! Is there a better way to do this? I would like to do something like Load( "pacman" + "/*animation%4 + 1*/" ); but of course this doesn't work. How can I do something like this!?

Share this post

Link to post
Share on other sites
you can have a for loop like this:
void Load(std::string name,Image* I)
for (int i=0;i<4;i++)
ToStr will return the number as std::string (i didnt do (i+'\0') instead because i see that the number is 01 02 03 04 and not 1 2 3 4)
it will be better to load all the images only once (or every time you go to some place or click "Play Now" if the game is really large) rather than to load it every time you need to.


Share this post

Link to post
Share on other sites
Yes, just have an array of frames and a place holder to keep track of which frame in the array you're on and increment the placeholder each time. when it goes past the end of the array (in your current example, 4) set it back to zero (start of the array).

hopefully this should get the idea across :


// size of frame-array
int pacmanFramesSize = 4

Image pacmanFrames[] = new Image[ pacmanFramesSize ]

for( i = 0 ; i < pacmanFramesSize ; i++ )
pacmanFrames[ i ] = loadPacmanFrame( "pacman" + i + ".tga" )

// in-game frame position
int pacmanFramesPlace = 0

// ....

// in the game loop:

Frame currentFrame = pacmanFrames[ pacmanFramesPlace ];

if( pacmanFramesPlace >= pacmanFramesSize )
pacmanFramePlace = 0;

[Edited by - Timberl on November 21, 2005 8:07:30 AM]

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!