Advertisement Jump to content


This topic is now archived and is closed to further replies.


2d animations

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

Hey all, just a quick question, but how are some ways that people have handled 2d animation in a game?? i''m not talking about moving a bitmap around.... but moving an animated bitmap around, like when a character walks. Do people use a special file format? circular lists? Just curious to see how people are doing this. --Thanks Ninj4D4n

Share this post

Link to post
Share on other sites
I''m using one image file to store all the sprites next to each other. I have a sprite class that accepts arguments such as: sprite size (width and height), number of sprites, etc. This way the class can figure out how to index the sprites itself, so bounds aren''t exceeded. I also have a class for a "Character" object, which has an instance of the sprite class as a member variable. In this "Character" class, I define certain animation variables, such as walkStart = 10, walkEnd = 16, etc. The numbers correspond to cells of animation in the sprite class, effectively allowing the character class to calculate how to loop through the walking animation, or any other animation I define constants for. This way, the image files can differ if I want them to--I can make walking animations begin at cell 12 for one character and 10 for another. This is convenient because some characters don''t need to have as many animations as other (i.e. a trivial dog "character" walking vs. the human lead character walking. I''d want the human to have more walking frames than the dog, so his movement could be more detailed. ) Anyway, hope this helps. If anyone wants to suggest some improvements on MY style, please go ahead. This is my first time writing game code.

Share this post

Link to post
Share on other sites
Here''s how I''m handling sprite animation for my 2D sidescroller:

First off define a FRAME class, used to hold each frame of animation. It just consists of the image and a delay value for how long it will take until the next frame.

  class FRAME
ZImage *img;
double delay;

Then for each monster/baddie/sprite type define a new CspriteInfo class like this:

  class CspriteInfo  
void unLoad();
virtual ~CspriteInfo();

ZImage *images;
list<FRAME> *anims;
int numImages;

Then I have a Csprite class for each INSTANCE of the sprites. This holds the monster''s position and state. The Csprite class has a static array of CspriteInfo''s for each monster type. This means that the actual images and animations for each monster type will not be loaded and allocated a place in memory for each individual monster on screen--I can put 10,000 baddies on screen, as long as there are a limited number of types.

  class Csprite : public CsceneItem

Csprite(int idInit, int x, int y, int angleInit, int rotVelInit);
virtual ~Csprite();

virtual void process();
virtual void draw();
void setAnim(int n);
void doAnim();
ZImage* getImage();

static CspriteInfo* spriteData;
static int numSprites;
static int spriteCount;

static void unLoadInfo();
static void addSpriteInfo(char * filename);
static void loadImages(int spriteId, int numImages, char *fileName,int r,int g, int b);

int id;
int currentImage;
list<FRAME>::iterator currentFrame;

int currentAnim;// which animation is current looping

int oldTime; // used for timing animation

double angle; // angle to draw at

double rotVel; // rotating velocity

POINTD_ pos;
bool deleteFlag;


The Csprite::doAnim() function looks like this:

  void Csprite::doAnim()
ZEngine *ze = ZEngine::GetInstance();

// see if we need to advance animation

if ( ze->GetTime() - oldTime > currentFrame->delay )
if (currentFrame == spriteData[id].anims[currentAnim].end())
currentFrame = spriteData[id].anims[currentAnim].begin();

oldTime = ze->GetTime();


So currentFrame is the key to the whole thing--its an STL list iterator, and it makes the sprite''s draw function REALLY easy. Each ZImage structure has Draw and DrawRotated functions, so:

  void Csprite::draw()

Share this post

Link to post
Share on other sites

That''s very helpful, so apparently there''s no magical sprite animation file, its up to me to make the images change accordingly. If anyone else has some input as to how they handle this, it would help alot. Thanks again thehurricane and fisheyel83l.


Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. 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!