Skip to Main Content
Learning XNA 3.0
book

Learning XNA 3.0

by Aaron Reed
November 2008
Beginner content levelBeginner
510 pages
16h 24m
English
O'Reilly Media, Inc.
Content preview from Learning XNA 3.0

Loading and Drawing Your Sprite

Now that you have an image loaded into your solution that is being recognized by the content pipeline, you're ready to draw it on the screen. But before you can access them in code, resources need to be loaded from the content pipeline into variables that you can use to manipulate them.

The default object used to store an image is Texture2D. Go ahead and add a Texture2D variable to your game in the Game1.cs file near the variable declarations for your GraphicsDeviceManager and SpriteBatch:

Texture2D texture;

Now, you'll need to load the actual image file into your Texture2D variable. To access data from the content pipeline, you use the Content property of the Game class. This property is of the type ContentManager and provides access to all objects loaded in the content pipeline. The ContentManager class has a Load method that will let you load content into different XNA object formats.

As mentioned previously, all loading of graphics, sounds, and other content resources should be done within the LoadContent method. Add the following line to the LoadContent method:

texture = Content.Load<Texture2D>(@"Images/logo");

The parameter passed into the Content.Load method is the path to the image file, starting with the Content node in Solution Explorer. When used in relation to strings, the @ symbol causes the string that follows to be interpreted literally, with escape sequences ignored. So, the following two lines of code will create the exact same string: ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning XNA 4.0

Learning XNA 4.0

Aaron Reed
Beginning C# 7 Programming with Visual Studio 2017

Beginning C# 7 Programming with Visual Studio 2017

Benjamin Perkins, Jacob Vibe Hammer, Jon D. Reid

Publisher Resources

ISBN: 9780596154905Supplemental ContentErrata Page