Skip to main content
GameDev.net gamedev.net
🔒 Locked

Thoughts on sprite animation

Started by DividedByZero Jan 10, 2012 at 10:01 AM 7 replies 1.5k views
Original Post
DividedByZero
DividedByZero
Hi Guys,

I have been working on a framework for a game I am working on and I am now upto sprite animation.

So far, I have sprites stored as structs in a vector and this is working well.

All of the animation frames will be stored in a single bitmap (per sprite).

I' am trying to work out what would be the best way to handle animations programmatically.

In the end I would like to be able to call up the animation something like so;

animateSprite(WALK, MAINPLAYER);

Having something like this

WALK - frames 1 to 10
RUN - frames 1 to 20 etc...

What would be the best way to handle this? Another vector perhaps?

Any thoughts would be awesome.
DividedByZero
DividedByZero
Somehow this triple posted. Please answer on this thread (I requested the other threads to be deleted).

Ta
Florian22222
Florian22222
maybe you should use an animateSprite(WALK,MAINPLAYER,fElapsedTime) method. You´ll call it in the update() method and the method will choose the right picture to display. e.g.:
delayPerPicture = 100ms
fElapsedTime = 0 -> picture[0]
fElapsedTime = 140 -> picture[1]
and so on.

it´s better to use oop here. a player class. contains some animations(a vector) indicated by a constant like WALK.


I hope this could help you!
DividedByZero
DividedByZero
Hi IceBreaker23,

This is sort of along the line what I was thinking, too

So are you saying I should think about changing my vector of sprite structs to a vector of sprite classes and within that have a vector of animations?

BTW, thanks for your help so far
Florian22222
Florian22222

Hi IceBreaker23,

This is sort of along the line what I was thinking, too smile.png

So are you saying I should think about changing my vector of sprite structs to a vector of sprite classes and within that have a vector of animations?

BTW, thanks for your help so far smile.png


yes, that´s what i was thinking about
Johan!
Johan!
Of course it depends a bit on your method of drawing (API used and so on) but as long as what you want is to play "strips" of frames from an image, I don't really see the point of bringing in containers and such.
You could just have animations being represented by rows in the image so to change animation you just change at what height you sample from the image (assuming that the frame dimensions are the same for all animations, otherwise you naturally need to adjust to that).
A very simple way to do it could be something like this (assumes that all animations loop and that you have your own timer class):


void Player::setAnimation( AnimationType aniType )
{
m_aniType = aniType;
m_currentFrame = 0;
m_timer.start();
...
}



void Player::update()
{
m_timer.update();
if( m_timer.runningTime > m_frameUpdateInterval )
{
m_currentFrame++;
if( m_currentFrame > m_numFrames )
m_currentFrame = 0;
m_timer.restart();
}
}




void Player::draw()
{
Rect rectToDraw = Rect( m_currentFrame * FRAME_WIDTH, m_aniType * FRAME_HEIGHT, FRAME_WIDTH, FRAME_HEIGHT );
...
}
DividedByZero
DividedByZero
Not a bad idea. But, how would you handle the different types of animation?

One thing that comes to mind is to multiply aniType*currentFrame. So, then walk might be 1, run = 2, etc. In effect frames 1 to 10 walk, 2 to 20 run etc.

Does this sound feasable?
Johan!
Johan!
In my example the AnimationType (an enum) determines which row in the image, and thus which animation, will be used.
If we for example assume that the walk animation is located in the first row of the image, defining the AnimationType ANI_TYPE_WALK as 0 would set up the correct rectangle.

In the same way, ANI_TYPE_RUN could be equal to 1 which gives rectangles on the second row (see m_aniType * FRAME_HEIGHT).



What API/engine are you using for drawing?

DividedByZero
DividedByZero
Thanks for the advice.

I am using sprites within DirectX 9.0c.

So, your example would be easily implemented with what I have so far

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.