Game Development Reference
In-Depth Information
In Chapter 8, I described the bullet firing system used in Dungeon Maze Adventure as having a few
weaknesses. The biggest weakness was that the >qhhap class and game manager classes were com-
pletely dependent on one another. What's most important about this game is that, unlike Dungeon
Maze Adventure, the bullet objects don't know or care what happens to them after they're created.
And the document class that manages the game doesn't depend on them because of the use of events
for communication.
Let's look at how dispatching events works to manage the bullets on the stage.
Creating bullets
Here's how new bullets are added to the stage:
1. When new bullets are added to the stage, the >qhhap class dispatches a ^qhhap?na]pa` event:
2. The document class has a listener that listens for a ^qhhap?na]pa` event:
3. When the event occurs, the document calls its kj>qhhap?na]pa` event handler. It copies a ref-
erence to the bullet that triggered the event into an array called ^qhhapo:
The [^qhhapo array keeps a list of all the bullets currently on the stage. Figure 10-26 illustrates this
Main_SpaceShooter (Document Class)
3. The document class listens
for the event. When it hears it,
it pushes a reference to the bullet
that triggered it ("")
into the _bullets array.
_bullets Array
4. The _bullets array
keeps track of all the bullets
currently on the stage.
2. bulletCreated event bubbles
up to the stage.
Bullet Class
1. When a new bullet is created,
the bullet class dispatches a
bulletCreated event.
Figure 10-26. When bullets are created, they're added to the _bullets array.
Search Nedrilad ::

Custom Search