Game Development Reference
In-Depth Information
using Microsoft.Xna.Framework;
1
using Microsoft.Xna.Framework.Graphics;
2
3
4
class SpriteGameObject : GameObject
{
5
protected Texture2D sprite;
6
7
8
public SpriteGameObject(Texture2D spr, int layer = 0)
: base (layer)
9
{
10
sprite = spr;
11
}
12
13
14
public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
15
if (visible)
16
spriteBatch.Draw(sprite, this .GlobalPosition, Color.White);
17
}
18
}
19
Listing 14.1
A sprite-based game object
14.4.2 A List of Game Objects
The next type of game object that we want to introduce is the game object that
consists of a list of other game objects. This is a very useful type, because it allows
us to create hierarchical structures of game objects. For example, the 'root' game
object will need to be a list of other game objects, since it contains the background
sprite game object, as well as the playing field. The playing field itself will also be a
list of game objects because it contains an overlay, a grid, and a row selector object.
To represent a game object containing a list of other game objects, we introduce
a class called GameObjectList (see also Listing 14.2 ). This class inherits from the
GameObject class, so a list of game objects is, itself, also a game object. This way,
we can treat it as a normal game object and give it a position, a velocity, a drawing
layer, or a parent game object. In order to manage a list of game objects, we need to
add the following member to the class:
protected List<GameObject> gameObjects;
In the constructor, we initialize this variable, so that the list is ready to be filled with
game objects.
One of the goals of the GameObjectList class is to take care of the game objects
that are in its list. This means that, if we call the Draw method of a GameObjectList
instance, this instance will draw all the game objects that are in its list. The same
Search WWH ::




Custom Search