Ok, so I said that next time we would me creating a level editor, I lied. I have decided to leave creating the level editor to the end. This way we won’t have to keep adding to it all the way through. Instead we are going to create a sprite class. This will be our base class for our enemies, towers etc. It’s going to hold the basic data about our sprites e.g. there texture, position and so on.
First off create a new class called “Sprite.cs” and add our basic using statements:
From now on these need to be added to every new class we make. Ok, so let’s add some fields to the class.
All of these fields are quite self explanatory so I won’t bother explaining them. The reason all of these fields are protected is so all of the different classes that are going to be inherited from this class can access these fields whereas anything else will not have access to them.
Now what we need to do is add some properties like the sprites centre. The sprites centre needs to be accessible to things like towers so that they know if the sprite is in range etc.
So, now that we have all of our fields and properties set up we can move onto our sprites constructor. For each new sprite we want to be able to pass in it’s position an it’s texture.
Then we need to set up our fields based on the data being passed in.
texture = tex;
Again most of this is self explanatory. We are just initializing our fields values.
The sprite class has two methods, “Draw” and “Update” in the “Update” method all we do is update where the sprites centre is and all we do in the draw method is pass in a SpriteBatch and draw our texture.
Notice that these two methods are set as virtual. This is so and class that inherits from “Sprite.cs” can override these methods.
And that’s it for our sprite class. Next time we will be creating our enemy class.