SFML community forums

Help => Graphics => Topic started by: rcplusplus on July 09, 2012, 08:23:53 pm

Title: Animating a sprite independently from the main loop
Post by: rcplusplus on July 09, 2012, 08:23:53 pm
So i have a sprite that needs to flash when it gets hit, and for convenience purposes, let's say (theoretically) I have a flash method in the spaceship object class like so

class Spaceship: public Object {
public:
   void flash()
   {
      if (t < 1.5) {
         float opacityRatio = cos(3t);
         sprite.setColor(sf::Color(255,255,255,255*opacityRatio));
      }
   }
};
 

The problem is that how do I get the "t" value? The game loop is repeated multiple times and relies on a clock, so if I use the clock value (which gets reset ever iteration) for "t" , the whole flash animation will mess up. So how do I get the "t" value? Should I use static variables (however I have a feeling that implementation isn't very graceful), or should I animate it in a separate thread (although I'm not sure it's possible)? Games use effects like this all the time, so I'm sure there's an easy way... Any suggestions/solutions would be extremely helpful.
Title: Re: Animating a sprite independently from the main loop
Post by: Laurent on July 09, 2012, 08:30:52 pm
Quote
The game loop is repeated multiple times and relies on a clock
So you already have 99% of the job done:
void flash(sf::Time elapsed)
{
   m_accumulatedTime += elapsed.asSeconds();
   if (m_accumulatedTime < 1.5) {
      float opacityRatio = cos(3 * m_accumulatedTime);
      sprite.setColor(sf::Color(255,255,255,255*opacityRatio));
   }
}
Title: Re: Animating a sprite independently from the main loop
Post by: rcplusplus on July 09, 2012, 08:35:04 pm
m_accumulatedTime is most likely static correct?
Title: Re: Animating a sprite independently from the main loop
Post by: Laurent on July 09, 2012, 08:41:10 pm
No, just a member of the class. Each flash needs its own time, right? "Static" would mean that all flashes share the same time.
Title: Re: Animating a sprite independently from the main loop
Post by: Acrobat on July 09, 2012, 09:02:42 pm
Just use as member value.
P.S. i suggest to walk through some open-source frameworks, and find  how they did that. (try to look cocos2d-x)
Title: Re: Animating a sprite independently from the main loop
Post by: rcplusplus on July 09, 2012, 09:04:59 pm
That would work too! Thanks!

And one more thing, how do I restart the flash? If I simply reset m_accumulatedTime back to 0, it'll just keep flashing. Should I use a static bool in this case (for example called isFlashing)?
Title: Re: Animating a sprite independently from the main loop
Post by: Laurent on July 09, 2012, 09:27:08 pm
Yes, for example. Or you can delete the object since you don't need it anymore (what does a flash that doesn't flash do?).

But please stop putting "static" everywhere. The static keyword has a very specific role (well, two in fact), and it doesn't solve all problems. Especially not here.