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.