Game Development Reference
In-Depth Information
using Microsoft.Xna.Framework;
1
using Microsoft.Xna.Framework.Graphics;
2
using Microsoft.Xna.Framework.Media;
3
4
5
class SpriteDrawing : Game
{
6
GraphicsDeviceManager graphics;
7
SpriteBatch spriteBatch;
8
Texture2D balloon;
9
10
11
static void Main()
{
12
SpriteDrawing game = new SpriteDrawing();
13
game.Run();
14
}
15
16
17
public SpriteDrawing()
{
18
Content.RootDirectory = "Content";
19
graphics = new GraphicsDeviceManager( this );
20
}
21
22
23
protected override void LoadContent()
{
24
spriteBatch = new SpriteBatch(GraphicsDevice);
25
balloon = Content.Load<Texture2D>("spr_lives");
26
27
28
MediaPlayer.Play(Content.Load<Song>("snd_music"));
}
29
30
31
protected override void Draw(GameTime gameTime)
{
32
GraphicsDevice.Clear(Color.White);
33
spriteBatch.Begin();
34
spriteBatch.Draw(balloon, Vector2.Zero, Color.White);
35
spriteBatch.End();
36
}
37
}
38
Listing 4.2
A program that displays a sprite on a white background
Let us now look at how we can load a sprite from a file and store it somewhere
in the memory (using a variable). We will need this variable in several different
methods. In the LoadContent method, we have to load the sprite and store it in the
variable. In the Draw method, we can access the variable in order to draw the sprite
on the screen. Therefore, we declare this variable at the class body level, so that
Search WWH ::




Custom Search