Game Development Reference
In-Depth Information
double frequency = 7;
float _wavyNumber = (float)Math.Sin(_totalTime*frequency);
_wavyNumber = 0.5f + _wavyNumber * 0.5f; // scale to 0-1
_text.SetColor(new Color(1, 0, 0, _wavyNumber));
_totalTime += elapsedTime;
}
The total time keeps track of how long the state has been running. The number
keeps increasing and will eventually become so big that it wraps around to 0. It's
used to feed the sine function numbers that will produce a wave similar to the
one plotted previously. The sine wave is scaled so that it oscillates between 0 and
1 rather than - 1 and 1. The frequency is increased as well to make the pulse
occur more often. Run the code and check out the effect.
After pulsing in and out of sight, the next step is to have the text travel through a
garish rainbow of all the colors. Each color channel is assigned a different sine or
cosine wave and the strengths of each channel change over time.
public void Update(double elapsedTime)
{
double frequency = 7;
float _wavyNumberR = (float)Math.Sin(_totalTime*frequency);
float _wavyNumberG = (float)Math.Cos(_totalTime*frequency);
float _wavyNumberB = (float)Math.Sin(_totalTime+0.25*frequency);
_wavyNumberR = 0.5f + _wavyNumberR * 0.5f; // scale to 0-1
_wavyNumberG = 0.5f + _wavyNumberG * 0.5f; // scale to 0-1
_wavyNumberB = 0.5f + _wavyNumberB * 0.5f; // scale to 0-1
_text.SetColor(new Color(_wavyNumberR, _wavyNumberG,
_wavyNumberB, 1));
_totalTime += elapsedTime;
}
It's very easy to play with this code to get a wide variety of different effects. The
color channel isn't the only thing that can by modified using trigonometric
functions; the next example will alter the position of the text. To change the text
position a SetPosition method must be added to the Text class. To move
the text, every vertex that makes up every character must have its position
changed; the easiest way to do this is just to re-create all these characters at the
new position.
 
Search WWH ::




Custom Search