XNA-The Basics


Now, I assume that you have set up XNA. We’ll be going further and start creating our own tower defence game. However, before that I’ll explain some basic things about XNA to you so that we won’t have any hitches further in our journey.

  1. First of all, create a new project.
  2. Select Visual C# and then select XNA Game Studio 4.0
  3. Select Windows Game.
  4. Give some name to your project and click on OK.
  5. Your game will be created.

XNA

Now, when the project is created, you will see a C# file. The name will be Game1.cs.  Here you will be seeing five (6, if you count the constructor) methods:

  • Initialize
    Initialize method is a bit of a black sheep among these methods. It acts like a constructor. It is the first method to be executed by the XNA Framework. All initialization activities should be done in this method. Initialize calls the LoadContent method.
  • LoadContent
    Load Content method is called exactly once. All the the sprites which you want to load are to be loaded through this method. It is called by initialize. Its function is similar to the Initialize method. All that you can do in the initialize method, you can do here. However, this method is used rather than the Initialize method. Initialize method is rarely used.
  • UnloadContent
    This method unloads any content that is loaded by the Load Content method. Again, this method should be used only if your game’s performance is degrading due to many sprites being in memory. Small games almost always never use this method.
  • Update
    This is the most important method of all. All your game logic goes in this method. This method is called 60 times per second. This method updates the sprites on the screen according to the logic described in the method. Anything not related to drawing and related to gaming logic comes in this method.
  • Draw
    This is the most important method after Update. It draws sprites and other components to the screen. This is called variable number of times per second. The draw method uses Spritebatches to draw components on the screen. More on drawing later.

If you press F5, you will see a blue screen. This means you created a new project succesfully.

The XNA program flow is as follows:

  1. In your Solution Explorer window, you will see a file called Program.cs, which creates an object of your current game. This object calls the game’s Run() method.
  2. This run method calls the Initialize method of the Game1.cs class which inherits the Game classs.
  3. The initialize method calls the Load Content method which then calls the Update method.
  4. After the update method, the draw method is called. For the rest of the execution, Update and Draw are interleaved in execution.

In next posts, I will tell you how to draw some simple textures onto the screen and how to make them move around. Once these basics are out of the way, then we can proceed to creating a tower defence game.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s