#1 Members - Reputation: 269
Posted 10 January 2012 - 04:01 AM
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.
#3 Members - Reputation: 391
Posted 10 January 2012 - 02:59 PM
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!
#4 Members - Reputation: 269
Posted 10 January 2012 - 03:17 PM
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
#5 Members - Reputation: 391
Posted 11 January 2012 - 12:30 AM
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
yes, that´s what i was thinking about
#6 Members - Reputation: 96
Posted 11 January 2012 - 12:06 PM
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 );
...
}
#7 Members - Reputation: 269
Posted 12 January 2012 - 02:45 AM
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?
#8 Members - Reputation: 96
Posted 12 January 2012 - 03:58 AM
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?






