|
| 1 | +--- |
| 2 | +title: Drawing a Masked Sprite over a Background |
| 3 | +description: Demonstrates how to draw a foreground and background sprite using the SpriteBatch class, where only part of the foreground sprite masks the background. |
| 4 | +requireMSLicense: true |
| 5 | +--- |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +In this example, you will draw a background texture, followed by another sprite on top of the background with its transparent elements not showing by using a [Blend State](xref:Microsoft.Xna.Framework.Graphics.BlendState) that supports alpha blending. |
| 10 | + |
| 11 | +### End result |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +## Requirements |
| 16 | + |
| 17 | +The example assumes the texture you are loading contains multiple images, one for the background and one for the foreground ship texture. |
| 18 | + |
| 19 | +-  |
| 20 | +-  |
| 21 | + |
| 22 | +Save the textures to your content project and name it "**AnimatedCharacter**" (this name will used to reference it in the project). |
| 23 | + |
| 24 | +> [!IMPORTANT] |
| 25 | +> The foreground sprite in this example must include masking information, e.g. a PNG or DDS file that supports transparency / an alpha channel. |
| 26 | +
|
| 27 | +## Drawing a Foreground and Background Sprite |
| 28 | + |
| 29 | +1. Follow the steps of [How To: Draw a Sprite](HowTo_Draw_A_Sprite.md). |
| 30 | + A good first step to understanding the loading and drawing of textures and setting up your project. |
| 31 | + |
| 32 | +1. Add some variables and update the **LoadContent** method to load and initialize the content. |
| 33 | + |
| 34 | + ```csharp |
| 35 | + // Position of foreground sprite on screen |
| 36 | + private Vector2 ViperPos; |
| 37 | + |
| 38 | + // The texture for the ship |
| 39 | + private Texture2D shipTexture; |
| 40 | + |
| 41 | + // The texture for the background |
| 42 | + private Texture2D starTexture; |
| 43 | + |
| 44 | + protected override void LoadContent() |
| 45 | + { |
| 46 | + // Create a new SpriteBatch, which can be used to draw textures. |
| 47 | + spriteBatch = new SpriteBatch(GraphicsDevice); |
| 48 | + |
| 49 | + starTexture = Content.Load<Texture2D>("starfield"); |
| 50 | + shipTexture = Content.Load<Texture2D>("ship"); |
| 51 | + Viewport viewport = graphics.GraphicsDevice.Viewport; |
| 52 | + |
| 53 | + ViperPos.X = viewport.Width / 2; |
| 54 | + ViperPos.Y = viewport.Height - 100; |
| 55 | + } |
| 56 | + ``` |
| 57 | + |
| 58 | +1. In [Game.Draw](xref:Microsoft.Xna.Framework.Game#Microsoft_Xna_Framework_Game_Draw_Microsoft_Xna_Framework_GameTime_) method of your game class, call [SpriteBatch.Begin](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch#Microsoft_Xna_Framework_Graphics_SpriteBatch_Begin_Microsoft_Xna_Framework_Graphics_SpriteSortMode_Microsoft_Xna_Framework_Graphics_BlendState_Microsoft_Xna_Framework_Graphics_SamplerState_Microsoft_Xna_Framework_Graphics_DepthStencilState_Microsoft_Xna_Framework_Graphics_RasterizerState_Microsoft_Xna_Framework_Graphics_Effect_System_Nullable_Microsoft_Xna_Framework_Matrix__) for the [SpriteBatch](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch). |
| 59 | + |
| 60 | +1. Specify [BlendState.Opaque](xref:Microsoft.Xna.Framework.Graphics.BlendState). |
| 61 | + |
| 62 | + > [!NOTE] |
| 63 | + > This will tell the [SpriteBatch](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch) to ignore alpha color values when drawing sprites. By default, the z-order of sprites is the order in which they are drawn. |
| 64 | + |
| 65 | +1. Call the [Draw](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch) method, passing in the `starTexture`. Then call [SpriteBatch.End](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch#Microsoft_Xna_Framework_Graphics_SpriteBatch_End). |
| 66 | + |
| 67 | + ```csharp |
| 68 | + public override void Draw (GameTime game) |
| 69 | + { |
| 70 | + GraphicsDevice.Clear(Color.CornflowerBlue); |
| 71 | + |
| 72 | + _spriteBatch.Begin(blendState: BlendState.Opaque); |
| 73 | + _spriteBatch.Draw (starTexture); |
| 74 | + _spriteBatch.End(); |
| 75 | + } |
| 76 | + ``` |
| 77 | + |
| 78 | +1. After this code, call [SpriteBatch.Begin](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch#Microsoft_Xna_Framework_Graphics_SpriteBatch_Begin_Microsoft_Xna_Framework_Graphics_SpriteSortMode_Microsoft_Xna_Framework_Graphics_BlendState_Microsoft_Xna_Framework_Graphics_SamplerState_Microsoft_Xna_Framework_Graphics_DepthStencilState_Microsoft_Xna_Framework_Graphics_RasterizerState_Microsoft_Xna_Framework_Graphics_Effect_System_Nullable_Microsoft_Xna_Framework_Matrix__) for the [SpriteBatch](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch) again. This time, specify [BlendState.AlphaBlend](xref:Microsoft.Xna.Framework.Graphics.BlendState). |
| 79 | + |
| 80 | + This will cause pixels on the sprite with an alpha value less than 255 to become progressively transparent based on the magnitude of the alpha value. An alpha of 0 will make the pixel completely transparent. |
| 81 | + |
| 82 | + > [!IMPORTANT] |
| 83 | + > Calling [SpriteBatch.Begin](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch#Microsoft_Xna_Framework_Graphics_SpriteBatch_Begin_Microsoft_Xna_Framework_Graphics_SpriteSortMode_Microsoft_Xna_Framework_Graphics_BlendState_Microsoft_Xna_Framework_Graphics_SamplerState_Microsoft_Xna_Framework_Graphics_DepthStencilState_Microsoft_Xna_Framework_Graphics_RasterizerState_Microsoft_Xna_Framework_Graphics_Effect_System_Nullable_Microsoft_Xna_Framework_Matrix__) with no parameters causes [SpriteBatch](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch) to **default to [BlendState.AlphaBlend](xref:Microsoft.Xna.Framework.Graphics.BlendState)**. |
| 84 | + |
| 85 | +1. Next in the [Draw](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch) method, we draw the `shipTexture`, `ViperPos` with [Color.White](xref:Microsoft.Xna.Framework.Color), finishing off with a call to [SpriteBatch.End](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch#Microsoft_Xna_Framework_Graphics_SpriteBatch_End). |
| 86 | + |
| 87 | + ```csharp |
| 88 | + public override void Draw (GameTime game) |
| 89 | + { |
| 90 | + _spriteBatch.Begin(blendState: BlendState.Opaque); |
| 91 | + _spriteBatch.Draw (starTexture); |
| 92 | + _spriteBatch.End(); |
| 93 | + |
| 94 | + _spriteBatch.Begin(blendState: BlendState.AlphaBlend); |
| 95 | + _spriteBatch.Draw (shipTexture, ViperPos, Color.White); |
| 96 | + _spriteBatch.End(); |
| 97 | + } |
| 98 | + ``` |
| 99 | + |
| 100 | +The end result is a fixed / opaque background with a semi-transparent ship drawn on top for the player. You can of course experiment with layers / parallax transparent backgrounds behind the player too, the choice is up to you. |
| 101 | + |
| 102 | +### Extra Credit |
| 103 | + |
| 104 | +Try using this technique on top of the [How To Make A Scrolling Background](HowTo_Make_Scrolling_Background.md) guide for the beginnings of your very own space shooter :D |
| 105 | + |
| 106 | +## See Also |
| 107 | + |
| 108 | +- [Drawing a Sprite](HowTo_Draw_A_Sprite.md) |
| 109 | +- [How To Make A Scrolling Background](HowTo_Make_Scrolling_Background.md) |
| 110 | + |
| 111 | +### Concepts |
| 112 | + |
| 113 | +- [What Is a Sprite?](../../whatis/graphics/WhatIs_Sprite.md) |
| 114 | + |
| 115 | +### Reference |
| 116 | + |
| 117 | +- [SpriteBatch](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch) |
| 118 | +- [SpriteBatch.Draw](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch#Microsoft_Xna_Framework_Graphics_SpriteBatch_Draw_Microsoft_Xna_Framework_Graphics_Texture2D_Microsoft_Xna_Framework_Vector2_Microsoft_Xna_Framework_Color_) |
| 119 | +- [Texture2D](xref:Microsoft.Xna.Framework.Graphics.Texture2D) |
0 commit comments