Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Animating a sprite independently from the main loop  (Read 2413 times)

0 Members and 1 Guest are viewing this topic.

rcplusplus

  • Newbie
  • *
  • Posts: 15
    • View Profile
Animating a sprite independently from the main loop
« 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Animating a sprite independently from the main loop
« Reply #1 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));
   }
}
Laurent Gomila - SFML developer

rcplusplus

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Animating a sprite independently from the main loop
« Reply #2 on: July 09, 2012, 08:35:04 pm »
m_accumulatedTime is most likely static correct?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Animating a sprite independently from the main loop
« Reply #3 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.
Laurent Gomila - SFML developer

Acrobat

  • Full Member
  • ***
  • Posts: 153
    • View Profile
Re: Animating a sprite independently from the main loop
« Reply #4 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)

rcplusplus

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Animating a sprite independently from the main loop
« Reply #5 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)?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Animating a sprite independently from the main loop
« Reply #6 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.
Laurent Gomila - SFML developer