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.
- First of all, create a new project.
- Select Visual C# and then select XNA Game Studio 4.0
- Select Windows Game.
- Give some name to your project and click on OK.
- Your game will be created.
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 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.
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.
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.
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.
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:
- 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.
- This run method calls the Initialize method of the Game1.cs class which inherits the Game classs.
- The initialize method calls the Load Content method which then calls the Update method.
- 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.