Jump to content
  • Advertisement
Sign in to follow this  
DividedByZero

Thoughts on sprite animation

This topic is 2530 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 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.

Share this post


Link to post
Share on other sites
Advertisement
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!

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites

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 :)

Share this post


Link to post
Share on other sites
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 );
...
}

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

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!