Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Thoughts on sprite animation


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 lonewolff   Members   -  Reputation: 269

Like
0Likes
Like

Posted 10 January 2012 - 04:01 AM

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.

Ad:

#2 lonewolff   Members   -  Reputation: 269

Like
0Likes
Like

Posted 10 January 2012 - 04:05 AM

Somehow this triple posted. Please answer on this thread (I requested the other threads to be deleted).

Ta :)

#3 IceBreaker23   Members   -  Reputation: 391

Like
0Likes
Like

Posted 10 January 2012 - 02:59 PM

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!

#4 lonewolff   Members   -  Reputation: 269

Like
0Likes
Like

Posted 10 January 2012 - 03:17 PM

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

#5 IceBreaker23   Members   -  Reputation: 391

Like
0Likes
Like

Posted 11 January 2012 - 12:30 AM

Hi IceBreaker23,

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

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 Posted Image


yes, that´s what i was thinking about :)

#6 Johan!   Members   -  Reputation: 96

Like
0Likes
Like

Posted 11 January 2012 - 12:06 PM

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 );
  ...
}


#7 lonewolff   Members   -  Reputation: 269

Like
0Likes
Like

Posted 12 January 2012 - 02:45 AM

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?

#8 Johan!   Members   -  Reputation: 96

Like
0Likes
Like

Posted 12 January 2012 - 03:58 AM

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?



#9 lonewolff   Members   -  Reputation: 269

Like
0Likes
Like

Posted 12 January 2012 - 04:21 AM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS