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:
lner]pabqj_pekjkj=``a`PkOp]ca$arajp6Arajp%6rke`
w
`eol]p_dArajp$jasArajp$^qhhap?na]pa`(pnqa%%7
y
2. The document class has a listener that listens for a ^qhhap?na]pa` event:
op]ca*]``ArajpHeopajan$^qhhap?na]pa`(kj>qhhap?na]pa`%7
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:
lner]pabqj_pekjkj>qhhap?na]pa`$arajp6Arajp%
w
[^qhhapo*lqod$Ikrea?hel$arajp*p]ncap%%7
y
The [^qhhapo array keeps a list of all the bullets currently on the stage. Figure 10-26 illustrates this
process.
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 ("event.target")
into the _bullets array.
op]ca*]``ArajpHeopajan$^qhhap?na]pa`(kj>qhhap?na]pa`%7
_bullets Array
lner]pabqj_pekjkj>qhhap?na]pa`$arajp6Arajp%
w
[^qhhapo*lqod$Ikrea?hel$arajp*p]ncap%%7
y
,*ejop]j_a15
-*ejop]j_a2/
.*ejop]j_a35
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.
`eol]p_dArajp$jasArajp$^qhhap?na]pa`(pnqa%%7
Figure 10-26. When bullets are created, they're added to the _bullets array.
 
Search Nedrilad ::




Custom Search