
First tutorial about XNA, it's going to cover simply how to draw a sprite onto the screen. So first start up C# express and create a new project using the Windows Game template, name it something simple like StubbsTutorial1.
Okay, go find a suitable picture to use for this tutorial, I used the picture at this location:
I just searched for "XNA" on google and found it. Save it and then add it to the project. Either rename the file to xna if it's not already xna.someExtension or change the name xna to whatever the name is when we load it in the initialization function in the code.
Alright, now open up the file Game1.cs, we need to create three variables that will let us draw the sprite.
SpriteBatch batch; //Required to draw the texture
Texture2D tex; //The texture to be drawn
Vector2 position2D; //The position of the texture
Spritebatch is needed to draw the sprites, we only need one. Then we create a Texture2D variable named tex, this will be the texture for our sprite. Then we have the Vector2 position (floatx, floaty), this will tell the computer where to put the sprite on the screen when it's drawn.
Now we need to give these variables some data to hold.
Go to the initialize function in the same Game1 class. Put this code in there:
batch = new SpriteBatch(graphics.GraphicsDevice); //That's the spritebatch.
tex = content.Load
//change xna to whatever the name of the texture file is (example:@"texture")
//We don't need to put the extension, (xna.jpg is not in a folder by the way).
position2D = new Vector2(200, 200); //X pos is 200, Y pos is 200.
position2D = new Vector2(200, 200); //X pos is 200, Y pos is 200.
So we have finished making the spritebatch, we loaded the texture from the file xna.jpg, and we chose where to put the sprite when we draw it.
Pretty easy huh? Now all that's left to do is to actually tell the computer to draw it.
So, go to the Draw function of the Game1 class.
Put this code in before the line base.Draw(gameTime);
batch.Begin();
We start the batch, whatever is in between batch.Begin() and batch.End() will be drawn, it will give an error if you did batch.Draw() outside of Begin and End.
batch.Draw(tex, position2D, Color.White);
We draw the sprite using tex as the texture, position2D as the x,y position, and we have no color tint which means we use Color.White.
batch.End();
We here end the batch so no more drawing.
Run it, you should get a picture in the window.
5 comments:
Hi.
Thanks a lot for the "XNA Tutorials", but i have the problem with Tutorial 1:
Error 1 The type arguments for method "Microsoft.Xna.Framework.Content.ContentManager.Load< T >(string)" cannot be inferred from the usage. Try specifying the type arguments explicitly.
tex = content.Load(@"xna");
Help me.
Valeriy
solution:
Go to the LoadGraphicsContent function in the same Game1 class. Put this code in after the line
// TODO: Load any ResourceManagementMode.Manual content
tex = Texture2D.FromFile(graphics.GraphicsDevice, @"xna.jpg");
Sorry
Valeriy
You can also use
tex = Content.Load Texture2D(@"Textures\xna");
with < > around Texture2D
In my case, I had my xna in a folder called 'Textures'.
kabily website irony gametes safeguard blogsblogs catalysts lucrative provable telegraphic takings
lolikneri havaqatsu
Thanks! This helped so much! I've seen a couple
rather confusing websites lately, this cleared up some confusion I had.
Post a Comment