Socoder -> Cobra -> Nice fading effect
|Posted : Tuesday, 28 June 2011, 07:06|
If you have read my dev diary recently you will see I have done some bitmap fonts.
I wanted to create a nice fading effect to add a little class to it.
Problem is my maths wasn't up to scratch last night and Im now suffering from coders block.
I will paste my code in a spoiler in a sec but here is a brief overview.
I use lists to track new entities of text to fade. and tracking the milliseconds it started and issuing each one a TTL so I can calculate how far along its life span it is.
only problem is my calculations are off, I can get it to fade in but fading out was not working, I just couldnt get the maths right.
I hope someone can help.
> Reveal 🔎
|Posted : Tuesday, 28 June 2011, 10:08|
HoboBen will help ya
|Posted : Tuesday, 28 June 2011, 10:27|
This bit looks suspect (line 222 and 227):
Considering Millisecs and textfade.start are both integers, dividing by 1000 is going to give you an integer second.
Other than that though, the fadetext function looks more complicated than it needs to be. I'd split it into two procedures with a direction field in the textfade type to simplify the logic.
|Posted : Tuesday, 28 June 2011, 13:54|
I told you codeing while tired is ugly!
I also had the same thought today.
A flag to show which way to fade, but I'm still struggling to code it
|Posted : Tuesday, 28 June 2011, 15:20|
Keep it simple to start with - just write a function that writes the alpha value to the screen, until you're happy that you've got a value that goes 0.0 to 1.0 to 0.0 properly.
Otherwise, it's hard to pinpoint the problem.
Think of it this way:
For the fade in,
At an offset of 0 ms (the start), you want alpha multiplied by 0.0
At an offset of LENGTH ms (the end), you want alpha multiplied by 1.0
Assume the fade takes 1s, so LENGTH = 1000
So at any point,
offset = Millisecs() - start_of_fade
alpha = offset / LENGTH
At start: offset = 0, alpha = 0 / LENGTH = 0
After 0.5s: offset = 500 ms, alpha = 500 / LENGTH = 0.5
After 1s: offset = 1000, alpha = 1000 / LENGTH = 1.0
For the opposite,
alpha = 1.0 - (offset / LENGTH)
At start: offset = 0, alpha = 1.0 - (0 / LENGTH) = 1.0 - 0 = 1
After 0.5s: offset = 500 ms, alpha = 1.0 - (500 / LENGTH) = 1.0 - 0.5 = 0.5
After 1s: offset = 1000 ms, alpha = 1.0 - (1000 / LENGTH) = 1.0 - 1.0 = 0.0
Now that you have these values, multiply the end result by 255 to get a usable value.
Remember that alpha and offset should both be reals.
As an alternative to maintaining a separate fadein/fadeout flag, you could have alpha in a range 0.0 to 2.0. When alpha > 1.0, make alpha = 2.0 - alpha and you have a fade out.
|Posted : Friday, 01 July 2011, 09:37|
You could also do something like this.
That's assuming your need an 'alpha' value between 0.0 and 1.0. If you need a percent then multiply 'alpha' by 100. If you nee d a value between 0-255 then multiply 'alpha' by 255.
|Posted : Monday, 04 July 2011, 03:55|
I have been away for 4 days so havn't coded or even look on here.
Thanks for that Andy, I managed to get it working with the help of HoboBen, it seems I was close but not quite with my work.
I will have a look over your code and see if I can incorporate it.
but first I need to get over this sunburn *ouch*