Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

How To: Tint a Sprite

This article demonstrates how to tint a sprite using a Color value.

The Complete Sample

The code in this tutorial illustrates the technique described in the text. A complete code sample for this tutorial is available for you to download, including full source code and any additional supporting files required by the sample.

To draw a tinted sprite on screen

  1. Follow steps 1–4 of How To: Draw a Sprite.
  2. In the Update method, determine how to tint the sprite. In this example, we take the value of the game pad thumbsticks and determine Red, Green, Blue, and Alpha values to apply to the sprite.

    protected Color tint;
    protected override void Update(GameTime gameTime)
    {
        ...
        GamePadState input = GamePad.GetState(PlayerIndex.One);
        tint = new Color(GetColor(input.ThumbSticks.Left.X),
            GetColor(input.ThumbSticks.Left.Y),
            GetColor(input.ThumbSticks.Right.X),
            GetColor(input.ThumbSticks.Right.Y));
    
        base.Update(gameTime);
    }
    
  3. In the Draw method, pass the color value created in Update to SpriteBatch.Draw.

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
    
        spriteBatch.Begin();
        spriteBatch.Draw(SpriteTexture, position, tint);
        spriteBatch.End();
    
        base.Draw(gameTime);
    }
    
  4. When all of the sprites have been drawn, call End on your SpriteBatch object.

Community Additions

Show:
© 2015 Microsoft