[TuT]Xna 3.1 Tutorial: Sprites

Posts 1–7 of 7 · Page 1 of 1
[TuT]Xna 3.1 Tutorial: Sprites
He everyone, I'm going to give you a short, simple tutorial on how to:
- Load Sprites to the Content Pipeline
- Store the Sprite using Texture2D
- Load the sprite into the LoadContent Method
- Draw the sprite onto the Screen

1. Easily Create a new project from the main menu screen, I don't care what you name it.
Code:
File > Create New Project > "Enter Name" > OK
2. Now we will load our sprites that we have already downloaded/made. I will be using a somewhat smiley face.
Code:
Solution Explorer > Right Click content > New > Existing Item > Browse for the item than add it

3. Since we have our content in the content pipeline we will need to store it somewhere in the code to keep all the assets and proporties for it. We will do this by placing the line of code below in your Game1 class.
Code:
Texture2D playerTexture;

4. Now we have our variable that will hold all our stuff we need for that sprite, we will have to use the LoadContent Method, to tell the program to load the information necessary avoiding a lot of code.
Code:
playerTexture = content.load<texture2d>("player")

The syntax is [ object = content.load<object type>("asset name") ]
To find the asset name it will be in the properties of the sprite where it says 'asset name':

5. We have to go ahead to write the code to draw the sprite from our content that we loaded. Before you decide to draw the sprite, you will need to know the position where you want to draw it. You will do this by placing another variable in your Game 1 class:
Code:
Vector2 playerPosition = new Vector2(100,100)
This code is telling us that we want to create a new variable with the name playerPosition, and tell it what the coordinates are. XNA Goes from 0,0 -> 800x600. It does not go negative.

6. Okay, we have the Position and the Sprite already to go lets draw it on the screen. We will do this by going to the Draw method and placing this code:
Code:
spriteBatch.begin(); //telling to start drawing
spriteBatch.draw(playerTexture, playerPosition, color.white);
spriteBatch.end(); //stop drawing
By telling xna Draw method we want to draw using 3 arguments which are the texture(the variable hold the sprites info), the vector(where it is going to show up on the x, and y axis, and to use the color white so no clipping or shading appears incorrect.


Thanks for reading.
Tutorial 2: Character movement will be out soon!
If you need more in-depth information on certain things that I use while programming please PM so we don't have to clutter the thread!
Constructive criticism is a +
Looks good, nice, but instead of screenshots of the code, use code tags
And.. Do not initialize those vars outside of the class. Like:

Code:
Vector2 playerPosition = new Vector2(100, 100);
Use Initialize() or Loadcontent(), but good would be Initialize().
Quote Originally Posted by 'Bruno View Post
Looks good, nice, but instead of screenshots of the code, use code tags
And.. Do not initialize those vars outside of the class. Like:

Code:
Vector2 playerPosition = new Vector2(100, 100);
Use Initialize() or Loadcontent(), but good would be Initialize().
Thanks for reading it. The vars and objects are declared in the class.
I declare them before the methods so that they are automatically public
variables so I don't have to write public in front of everyone and it is
easier to read.
Quote Originally Posted by Fantomfury26 View Post
Thanks for telling me that, I mean but I was taught to use it right inside the class, I don't know why but it works.
I meant, not to initialize them where you declare them (it's always inside the class), and yea it works... but..
Yeah, I went to class today. I'm doing my C++ class in JMU. I asked my teacher he said that you shouldn't put it in the class, it's best off in the method that you are initializing it. But in a smaller game/program it would be easier to just declare them in the class.
Quote Originally Posted by Fantomfury26 View Post
Yeah, I went to class today. I'm doing my C++ class in JMU. I asked my teacher he said that you shouldn't put it in the class, it's best off in the method that you are initializing it. But in a smaller game/program it would be easier to just declare them in the class.
exactly good job anyway ^^
Quote Originally Posted by 'Bruno View Post
exactly good job anyway ^^
/ Thank you very much!
Posts 1–7 of 7 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?