Robot Foot Games Helping you improve your games as we improve our own. :)

30May/110

Blending through multiple colors

This is an update to a post I made beforehand but it has been made much easier to modify/customize now so it's coming at you with a new post!

Someone in the XNA forums brought up that they wanted to change through the colors of the rainbow, and have them blend together as they go through. He brought up how Mario does it when you touch a star, it cycles through a bunch of colors, and I noticed this while I was playing through Retrofit - Overload. The enemies are all colored, and as they get closer to dying they lose their colors and turn to white. I thought this was a neat effect because you don't need a health bar to show all of the enemy's health, you can use a visual representation and it works just as good, if not better, and doesn't clutter up your GUI!

Here's a video showing off the color changer in action (exciting, I know):

 

The entire class is just below. Read through it quick (its not very long) so you're not lost when I explain it underneath. :)

public sealed class ColorChanger
{
    private readonly Color[] colors;
    private float elapsed;
    private int nextColorIndex = 1;
    private readonly float timeBetweenColors;
    /// <summary>
    /// Gets the current color of the object
    /// </summary>
    public Color CurrentColor { get; private set; }
    /// <summary>
    /// Get or Set whether or not the ColorChanger is active.
    /// </summary>
    public bool IsActive { get; set; }
    public ColorChanger(float timeBetweenColors, bool isActive, params Color[] colors)
    {
        this.colors = colors;
        this.timeBetweenColors = timeBetweenColors;
        this.IsActive = isActive;
        Reset(IsActive);
    }
    public void Reset(bool isActive)
    {
        //Set our initial color. If no colors OR we're not active
        //we set the CurrentColor to White. Otherwise we'll use
        //the first color in our list of 'colors'
        if (!IsActive || colors.Length <= 0)
            CurrentColor = Color.White;
        else
            CurrentColor = colors[0];
        nextColorIndex = 1;
        elapsed = 0f;
        IsActive = isActive;
    }
    public void Update(GameTime gameTime)
    {
        //If we deactivate the ColorChanger or we only
        //use a single color then we don't need to update
        if (!IsActive || colors.Length == 1)
            return;
        //Keep track of the time so we know when to change colors
        elapsed += (float)gameTime.ElapsedGameTime.TotalSeconds;
        //If it's time to change colors, do so!
        if (elapsed >= timeBetweenColors)
        {
            //If we've passed the last color, start at the beginning
            if (++nextColorIndex >= colors.Length)
                nextColorIndex = 0;
            //Reset the time so we can start over again
            elapsed = 0f;
        }
        //Interpolate our CurrentColor through our list of colors!
        CurrentColor = Color.Lerp(
            CurrentColor,
            colors[nextColorIndex],
            elapsed / timeBetweenColors);
    }
}

 

OK, so now lets get on to implementation! After you get the class added to your project you'll want to add a ColorChanger variable to the top of your class:

ColorChanger colorChanger;

and you'll want to initialize it in your LoadContent(...) method:

colorChanger = new ColorChanger(
    1f, //Time between color changes
    true, //Is the color changer active?
    Color.Red, Color.Orange, Color.Yellow, Color.Green, //All the colors we want to cycle through
        Color.Blue, Color.Indigo, Color.Violet);

and make sure to update it in our Update(...) method:

colorChanger.Update(gameTime);

 

That does it for setting it up. Here we're just setting it to active right away so it'll continue to update and change colors. There's a good chance you'd only want this to be active after you pick up a powerup or something so you can initialize it as inactive and then call "colorChanger.IsActive = true;" when you want to activate it.

Awesome, it's setup, but how do I use it? You can grab the current color of the color changer by calling "colorChanger.CurrentColor". If the color changer is inactive it'll simple return Color.White and it won't go through updating itself until it's set to active. Here's an example of a SpriteBatch call you might use:

spriteBatch.Draw(texture, position, colorChanger.CurrentColor);
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(required)

No trackbacks yet.