Sunday, April 8, 2007

XNA tutorial 1: Drawing a sprite.

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(@"xna");

//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.

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);


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.


We here end the batch so no more drawing.

Run it, you should get a picture in the window.


Anonymous said...

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.

Anonymous said...

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");


Perseus109 said...

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'.

Anonymous said...

kabily website irony gametes safeguard blogsblogs catalysts lucrative provable telegraphic takings
lolikneri havaqatsu

Anonymous said...

Thanks! This helped so much! I've seen a couple
rather confusing websites lately, this cleared up some confusion I had.

Anonymous said...

Thanks a lot for sharing this with all folks you actually know what
you are speaking approximately! Bookmarked. Kindly additionally visit my site =).
We will have a hyperlink trade agreement between us
Look into my site :: DJ Laptop Cases

Anonymous said...

I think this is among the most important info for me.
And i am glad reading your article. But should remark on few general things,
The site style is perfect, the articles is really excellent : D.
Good job, cheers
My site - Marc Primo

Anonymous said...

I am really impressed along with your writing skills as
smartly as with the layout in your weblog. Is that this a
paid topic or did you customize it your self?
Either way keep up the nice high quality writing, it's rare to peer a great weblog like this one today..
My web-site - we buy ugly houses PA

Anonymous said...

It's amazing in favor of me to have a website, which is helpful in favor of my know-how. thanks admin
Here is my blog post :: pendulum castle star coins

Anonymous said...

When someone writes an piece of writing he/she retains
the idea of a user in his/her mind that how a user can understand it.
Thus that's why this paragraph is outstdanding. Thanks!
Also visit my weblog :: Como Conquistar A Una Amiga

Anonymous said...

Hello There. I found your blog using msn. This is a very well written article.
I'll make sure to bookmark it and come back to read more of your useful information. Thanks for the post. I'll certainly
Take a look at my webpage :: wealth definition

Anonymous said...

I used to be recommended this blog by means of my cousin.

I'm now not positive whether this publish is written by him as nobody else know such precise approximately my trouble. You are incredible! Thank you!
Also see my webpage - Interior Design Ideas

Anonymous said...

Your means of telling everything in this piece of writing is genuinely good, all be able to effortlessly know it, Thanks a lot.
Also see my page: Apple Valley

Anonymous said...

Excellent way of telling, and fastidious
piece of writing to obtain information regarding my presentation focus, which i am going to convey in university.
Here is my site sell iraqi dinar

Anonymous said...

I am really enjoying the theme/design of your weblog.
Do you ever run into any internet browser compatibility issues?
A small number of my blog audience have complained about my site not operating correctly in Explorer but looks great in Safari.

Do you have any solutions to help fix this
Look at my site - Big adult dating site

Anonymous said...

Malaysia & Singapore & brunei finest online blogshop for wholesale & quantity korean add-ons, earrings, earstuds, locket,
rings, bracelet, hair & bracelet add-ons. Deal 35 % wholesale price cut. Ship Worldwide
My web-site :: staples kardus

Anonymous said...

Malaysia & Singapore & brunei ideal online blogshop for wholesale & quantity
korean accessories, accessories, earstuds, necklace, rings,
hair, bracelet & bracelet add-ons. Deal 35 % wholesale discount. Ship Worldwide
My web site :: stitching wire jakarta

Anonymous said...

Pretty component of content. I just stumbled upon your site and in accession capital to assert that I acquire actually loved account your weblog posts.
Any way I'll be subscribing for your augment and even I success you access constantly rapidly.
Feel free to surf my web-site cheap greenhouse

Anonymous said...

This is my first time visit at here and i am truly impressed to read everthing at alone place.
my web page :: simply click the following website page

Anonymous said...

Thanks for every other informative site. The place else
may I get that kind of info written in such
an ideal way? I have a mission that I am simply now working on,
and I have been on the look out for such info.
Feel free to surf my homepage ; endurance drink

Anonymous said...

Hey very cool blog!! Man .. Excellent .. Amazing .. I will
bookmark your web site and take the feeds additionally?
I'm glad to search out numerous useful info here in the post, we'd like develop more strategies on this regard, thank you
for sharing. . . . . .

my webpage - domains for sale

Anonymous said...

I am truly grateful to the owner of this
site who has shared this fantastic piece of writing at here.

Also visit my weblog - hakukoneoptimointia

Anonymous said...

Excellent blog here! Also your site a lot up fast!
What host are you the use of? Can I am getting your associate hyperlink
to your host? I desire my website loaded up as quickly as yours lol

Feel free to visit my site ... villas in Bodrum for sale

Anonymous said...

Asking questions are truly fastidious thing if you are not
understanding anything completely, but this piece of writing gives nice understanding

Check out my web page - life quotes

Anonymous said...

I pay a quick visit daily some ѕites anԁ websіteѕ to reаd articlеs or геvіews,
exсeρt this web site ρrovіԁes quality baseԁ cοntеnt.

Ϻy webpage Highly recommended Internet site

Anonymous said...

These are in fact impressive ideas in regarding blogging.
You have touched some good factors here. Any way keep up wrinting.

My blog post; buy adjustable dumbbells
Also see my web page: adjustable weight dumbbells

Anonymous said...

Grеat beаt ! I wish to aρprenticе even as you amenԁ your site, how cοuld i
subscribe for a blog websitе? The account helреd me a
acceρtable ԁeal. I have been a little bit aсquaіnteԁ οf
this your broadcast provided ѕhiny transparеnt idea

my web sitе - click through the following web site

Anonymous said...

It's actually a great and helpful piece of info. I am happy that you shared this useful info with us. Please keep us up to date like this. Thank you for sharing.

Here is my webpage: v2 cig coupons
My web site :: Http://dunnhighreunion80-85.ning.Com

Anonymous said...

I've been surfing online greater than three hours lately, but I never found any attention-grabbing article like yours. It is beautiful worth sufficient for me. Personally, if all site owners and bloggers made just right content as you did, the web will be much more useful than ever before.

Take a look at my web site :: More Support

Anonymous said...

Therе's definately a lot to find out about this topic. I really like all of the points you'νe made.

Mу web site - V2 Cigs Review

Anonymous said...

I visit day-to-day a few web pages and information sites to read articles, except this website presents quality based

Look into my page ... things to do in chiang mai

Anonymous said...

Thаt іs ѵеry attentіon-gгabbing, Үοu are аn оvегly ѕkilleԁ bloggег.
I've joined your rss feed and look forward to in search of extra of your wonderful post. Additionally, I've shaгeԁ your web sіte in mу
social netωoгks

Нerе іs my site

Anonymous said...

Hello there just came upon your website via Yahoo after I entered in, "Blogger: Stubbs.C#.XNA" or something similar (can't quite remember exactly). In any case, I'm grateful I
found it simply because your subject material is exactly what I'm looking for (writing a college paper) and I hope you don't mind if I gather some information from here and I
will of course credit you as the reference. Thanks for your time.

Feel free to surf to my website quality link building

Anonymous said...

Hello! My partner and I frequently publish guest articles or blog posts for
other site owners to help increase publicity to our work, as well as provide
terrific articles to website owners. It really is a
win win situation! If you happen to be interested feel free to contact me
at: so we may discuss further. Thank you!

my homepage ... how to buy auto insurance

Anonymous said...

I’m not that much of a internet reader to
be honest but your sites really nice, keep it up! I'll go ahead and bookmark your website to come back in the future. Cheers

my blog post: original site

Anonymous said...

Exploring I noticed your website bookmarked as:
Blogger: Stubbs.C#.XNA. I'm assuming you bookmarked it yourself and wanted to ask if social book-marking gets you a ton of targeted visitors? I've been
thinking of doing some bookmarking for a few of my sites but wasn't certain if it would yield any positive results. Many thanks.

Stop by my homepage: Going Here

Anonymous said...

Hi colleagues, nice paragraph and good urging commented at this place, I am really enjoying by these.

My web site - read the full info here

Anonymous said...

May I simply say what a comfort to discover a person that actually knows what they are talking about on the net.

You definitely realize how to bring a problem to light and make it important.
More and more people need to look at this and understand this side of your story.

It's surprising you're not more popular given that you certainly
have the gift.

Feel free to surf to my web site:

Anonymous said...

Thanks for finally writing about > "XNA tutorial 1: Drawing a sprite." < Liked it!

My page; www.

Anonymous said...

Spot on with this write-up, I really feel this site needs a great deal more attention.

I'll probably be back again to see more, thanks for the info!

Here is my weblog: real nude teen

Anonymous said...

Greetings! This is my first visit to your blog! We are a collection of volunteers and starting a
new project in a community in the same niche. Your blog provided us beneficial information to work on.
You have done a outstanding job!

Feel free to visit my web-site;

Anonymous said...

I've been browsing on-line greater than 3 hours lately, yet I never found any interesting article like yours. It is beautiful price sufficient for me. In my opinion, if all site owners and bloggers made just right content material as you did, the web will be much more useful than ever before.

Take a look at my blog ::

Anonymous said...

I’m not that much of a online reader to be honest but your
blogs really nice, keep it up! I'll go ahead and bookmark your site to come back down the road. Cheers

Also visit my web site: visit their website

Anonymous said...

Hmm is anyone else encountering problems with the pictures on this blog loading?
I'm trying to determine if its a problem on my end or if it's the blog.
Any responses would be greatly appreciated.

Visit my web page :: visit the website

Anonymous said...

Does your blog have a contact page? I'm having problems locating it but, I'd like to send you
an e-mail. I've got some creative ideas for your blog you might be interested in hearing. Either way, great site and I look forward to seeing it improve over time.

Also visit my page ... visit this page

Anonymous said...

I enjoy what you guys are usually up too. This type of clever
work and coverage! Keep up the fantastic works guys I've you guys to blogroll.

my webpage; state auto insurance companies

Anonymous said...

I do not know if it's just me or if everybody else encountering problems with your blog. It appears as though some of the text within your content are running off the screen. Can someone else please comment and let me know if this is happening to them as well? This might be a problem with my internet browser because I've
had this happen before. Appreciate it

Look into my blog post: instant auto insurance quote []

Anonymous said...

I really like what you guys are usually up too. This kind of clever work and
exposure! Keep up the excellent works guys I've added you guys to my blogroll.

My weblog - average auto insurance rates by state :: ::

Anonymous said...

Please let me know if you're looking for a author for your blog. You have some really great posts and I believe I would be a good asset. If you ever want to take some of the load off, I'd
absolutely love to write some content for your blog in exchange for
a link back to mine. Please send me an email if interested.


Also visit my web-site ... Read More In this article

Anonymous said...

bookmarked!!, I love your blog!

Also visit my web-site celebrity free latin porn ()

Anonymous said...

you're actually a excellent webmaster. The web site loading speed is incredible. It seems that you are doing any unique trick. In addition, The contents are masterwork. you have performed a fantastic activity in this matter!

Review my page :: marriage sex

Anonymous said...

Hi! I know this is kind of off topic but I was wondering which blog platform are you using for
this site? I'm getting fed up of Wordpress because I've had issues with hackers and I'm looking at options for another platform. I would be great if you could point me in the direction of a good platform.

Also visit my site: view publisher site (