Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Continuing from this question (new question - now unrelated)

So I have a thorough background in programming already (algorithms, math, logic, graphing problems, etc.) however I've never attempted to code a game before. In fact, I've never had anything more than minimal input from a user during the execution of a program. Generally input was given from a file or passed through console, all necessary functions were performed, then the program terminated with an output. I decided to try and get in on the world of game development. From several posts I've seen around gamedev.stackexchange.com XNA seems to be a favorite, and it was recommended to me when I asked where to start.

I've downloaded and installed Visual Studio 2010 along with the XNA Framework and now I can't seem to get moving in the right direction.

I started out looking on Google for "xna game studio tutorial", "xna game development beginners", "my first xna game", etc. I found lots of crap. The official "Introduction to Game Studio 4.0" gave me this (plus my own train of thought happily pasted on top thanks to MSPaint):

http://tinypic.com/r/2w1sgvq/7

The "Get Additional Help" link (my best guess, since there was no "Continue" or "Next" link) lead me to this page:

http://tinypic.com/r/2qa0dgx/7

I tried every page. The forum was the only thing that seemed helpful, however searching for "beginner", "newbie", "getting started", "first project", and similar on the forums turned up many threads with specific questions that are a bit above my level ("beginner to collision detection", for instance)

Disappointed I returned to the XNA Game Studio home page. Surely their own website would have some introduction, tutorial, or at least a useful link to a community. EVERYTHING on their website was about coding Windows Phone 7.... Everything.

http://tinypic.com/r/10eit8i/7

http://tinypic.com/r/120m9gl/7

Giving up on any official documentation after a while, I went back to Google. I managed to locate www.xnadevelopment.com. The website is built around XNA Game Studio 3.0, but how different can 3.0 be from 4.0?.... Apparently different enough.

http://tinypic.com/r/5d8mk9/7

http://tinypic.com/r/25hflli/7

Figuring that this was the correct folder, I right-clicked....

http://tinypic.com/r/24o94yu/7

Hmm... maybe by "Add Content Reference" they mean "Add a reference to an existing file (content)"? Let's try it (after all- it's my only option)

http://tinypic.com/r/2417eqt/7

At this point I gave up. I'm back. My original goal in my last question was to create a keyboard-navigable 3D world (no physics necessary, no logic or real game necessary). After my recent failures my goal has been revised. I want to display an image on the screen.

Hopefully in time I'll be able to move it with the keyboard.

share|improve this question
    
+1 I had some problems at first too. Keep going, you'll get the jist of it! =) –  Will Marcouiller Mar 4 '11 at 21:28
add comment

2 Answers

up vote 7 down vote accepted

Having created your first XNA game project, you will get two projects created.

  • MyFirstGame

This project contains:

  1. Game.ico;
  2. Game1.cs;
  3. GameThumbnail.png;
  4. Program.cs.

A reference to your content is also available. That is refered to as the content pipeline.

  • MyFirstGameContent

This project's content is primarily empty. You will have to add content to it, such as your sprites (images).

  • The Game1.cs class structure

This class inherits from the Microsoft.Xna.Framework.Game class.

The most important methods of this class are:

  1. private members;
  2. Its constructor;
  3. protected override void Initialize() { } ;
  4. protected override void LoadContent() { } ;
  5. protected override void Update(GameTime gameTime) { } ;
  6. protected override void Draw(GameTime gameTime) { } .
  • Private Members

The default private members that are useful:

private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch; // This instance of the SpriteBatch is used to display your sprites in your game.
private Texture2D _sprite; // The sprite that will be displayed on the game screen.
  • The constructor

You may here change the screen size, its resolution. When you wish to address specifically to Windows, you may even use the compiler directives:

#if WINDOWS
    _graphics.PreferredBackBufferHeight = 400;
    _graphics.PreferredBackBufferWidth = 640;
#endif

These lines will change your game resolution to the specified values.

  • The Initialize() method

You may or may not have anything to write here. Aside, if you wish to reserve some area for specific needs in your game, you may as well define your boundaries variables here.

  • The LoadContent() method

This is the most important method along with the Update() and Draw() methods. This is here you will need to load your sprite.

protected override void LoadContent() {
    // Remember those private members? They'll be used here!

    _spriteBatch = new SpriteBatch(GraphicsDevice);
    _sprite = Content.Load<Texture2D>(@"mySpriteKey");
}

The System.String "mySpriteKey" refers to the Asset Name property accessed through the Properties Window of an image file.

  • The Update(GameTime gameTime) method

This is used to update the state of your game and get the keys pressed by the user such as keyboard keys. That is subject for another question though. Besides, you need to master this method to make your sprite move on screen.

  • The Draw(GameTime gameTime) method

This is where the sprite, loaded within the LoadContent() method, gets drawn on your game screen. To draw your loaded in _sprite private member sprite image, you need to use the _spriteBatch private member.

protected override void Draw(GameTime gameTime) {
    GraphicsDevice.Clear(Color.CornFlowerBlue);

    _spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
    _spriteBatch.Draw(_sprite, new Rectangle(0, 0, 100, 100), Color.White);
    _spriteBatch.End();
}
  • Okay, I've all coded the necessary, but the
_sprite = Content.Load<Texture2D>(@"mySpriteKey");` 

line complains about an invalid asset name or something, what to do?

You need to add your sprite image file to your MyFirstGameContent project. In order to do so,

  1. right-click on the project name, and Add|Existing Item; then
  2. browse to your whatever image you want to display on screen; and
  3. click [Add] button.

You should now see you image file being part of your content project. Now,

  1. right-click the image file, and click Properties; you now have to make sure that
  2. your Asset Name property value is the same as the string key you passed to the Content.Load<T>() method.
  • Some interesting links I've used to learn
  1. Arkanoid Nintendo NES Sprites;
  2. How to make a 2D XNA Game;
  3. Going Beyond: XNA Game Studio in 3D.

Please feel free to ask any further details as I will be pleased to tell all I know and beyond. I love to share knowledge.

share|improve this answer
    
I was able to figure it out =) Thank you. I was confused since the folder was named "WindowsGame1 Content" and not "Content" (as all of the tutorials told me). I was able to Add Existing Item and display it on the screen. I've also been programming objects to move on their own =) Next step- reading the keyboard. –  stevendesu Mar 5 '11 at 5:03
    
I'm glad I could help once more. I hope this will help other GD friends as well! =) As for the detailed explanation, I had to make sure I cover the detail you missed, so I gave it all! ;-) As for the keyboard, I, in the Update() method, have stored the keys pressed in a Keys[] array, then I simply perform a switch(Key[0]) {} if the array's not null. I think I provided some examples within the links given herewith. –  Will Marcouiller Mar 5 '11 at 12:45
add comment

Here is a small sample from msdn on how to load and draw a simple sprite.

I would wrap the Content.Load part into a Method, so you could just call a custom method like, loadTexture("textureName") that loads the texture or sets other things up as well.

But that is not that helpful for a lot of sprites, or sprites with animations.

To use sprites with animations, use Sprite Sheet Packer , and here is a Sample that shows how to use it in XNA. It generates a Sprite Sheet, and a txt file with a map of rectangles for each image.

In my games, I create a AssetManager, that has a Method to load a SpriteSheet texture, and uses the exported text file to group animations. I just edited the txt file map to have the animations all grouped with no spaces under each line, and then a line break to end it and start the new one.

The reasons sprite sheets are really good to use in XNA, is that for each texture that is used for drawing, it requires an extra SpriteBatch call to the video card, and if you have too many calls, you will have reduced performance. So if you are going to have a lot of objects being drawn at once, if they can all fit on one sprite sheet, you will have less drawing overhead.

But if you have a large animation with little to no extra instances, you are going to want to import the texture as individual frames, so only each frame needs to be sent to video card, and not the entire animation when it is not being used. Here is an answer I posted explaining how to do the large animation

share|improve this answer
    
This tutorial didn't help me at all. First, it assumed that you already had a texture (they used "ship") in the Content Pipeline, which as I explained above I was unable to do from any tutorials I found. I did eventually figure out that where every tutorial says there's a folder named "Content", I had a folder named "WindowsGame1 Content". When I finally added a .PNG file and tried using the code in your sample to display it on the screen, I got the error Error loading "Smiley.png". File not found.. I tried Smiley and Smiley.png. It is in my Content folder, and the Asset Name is Smiley –  stevendesu Mar 4 '11 at 21:16
    
You should have a folder in the solutions explorer. You can drag the file from explorer to that folder. Then it should build fine. –  AttackingHobo Mar 4 '11 at 21:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.