Bagikan melalui


How to: Tint a Sprite

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

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 ) { ... // TODO: Add your update logic here. 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 );

}

  1. In the Draw method, pass the color value created in Update to SpriteBatch.Draw.

    protected override void Draw( GameTime gameTime )
    

{ graphics.GraphicsDevice.Clear( Color.CornflowerBlue );

// TODO: Add your drawing code here.
ForegroundBatch.Begin();
ForegroundBatch.Draw( SpriteTexture, position, tint );
ForegroundBatch.End();

base.Draw( gameTime );

}

  1. When all of the sprites have been drawn, call End on your SpriteBatch object.
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    ContentManager content;


    public Game1()
    {
        graphics = new GraphicsDeviceManager( this );
        content = new ContentManager( Services );
    }

    protected override void Initialize()
    {
        // TODO: Add your initialization logic here.

        base.Initialize();
    }


    protected Texture2D SpriteTexture;
    protected SpriteBatch ForegroundBatch;
    protected Vector2 position;
    protected override void LoadGraphicsContent( bool loadAllContent )
    {
        if (loadAllContent)
        {
            // TODO: Load any ResourceManagementMode.Automatic content.
            ForegroundBatch = new SpriteBatch( graphics.GraphicsDevice );
            SpriteTexture = content.Load<Texture2D>( "vipership" );
            Viewport viewport = graphics.GraphicsDevice.Viewport;
            position.X = (viewport.Width / 2) - (SpriteTexture.Width / 2);
            position.Y = (viewport.Height / 2) - (SpriteTexture.Height / 2);
        }

        // TODO: Load any ResourceManagementMode.Manual content.
    }


    protected override void UnloadGraphicsContent( bool unloadAllContent )
    {
        if (unloadAllContent == true)
        {
            content.Unload();
        }
    }


    protected Color tint;
    protected override void Update( GameTime gameTime )
    {
        // Allows the default game to exit on Xbox 360 and Windows.
        if (GamePad.GetState( PlayerIndex.One ).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        // TODO: Add your update logic here.
        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 );
    }
    private byte GetColor( float input )
    {
        input = Math.Abs( input );
        return (byte)(255 - (input * 255));
    }

    protected override void Draw( GameTime gameTime )
    {
        graphics.GraphicsDevice.Clear( Color.CornflowerBlue );

        // TODO: Add your drawing code here.
        ForegroundBatch.Begin();
        ForegroundBatch.Draw( SpriteTexture, position, tint );
        ForegroundBatch.End();

        base.Draw( gameTime );
    }
}

See Also

Tasks

How to: Draw a Sprite

Concepts

2D Graphics Overview

Reference

SpriteBatch
Draw
Texture2D
Color