Count up and down elegantly

I am trying to make a blinking object i.e. increase its alpha value from 0 to 255 (gradually), then go back to 0 and repeat.

Is there a way that I can do this without using some booleans? It's easy to increment it:

alpha = time.elapsed()%256;

      

But what's a good way to make him fall asleep again after that?

+2


source to share


3 answers


How about using the sin function, this way the fade becomes more pleasant and you get what you want.



+15


source


Perhaps you could do it like this:



alpha = abs((time.elapsed() % 510) - 254);

      

+13


source


abs (((x + 255)% 510) - 255) will go linearly from 0 to 255 for x between 0 and 255 and linearly from 255 to 0 for x between 255 and 510. Then it repeats (with a period of 510 of course) ...

+5


source







All Articles