The next application will demonstrate the usefulness of the factory method pattern
when designing fast-paced action games, as many sprites on the screen are created at
runtime based on user input. When an application doesn't know which objects to
create until runtime, the factory method design pattern light bulb should go off in
your head. We will develop a portion of a vertical shooter game in the best
traditions of the original Space Invaders. The game will be based on the sprite
factory example, as all interactive objects that appear on the Flash Stage are
derived form the
Sprite class. This will not be a
complete game, but the parts that show the utility of the factory method pattern,
such as creating different space ships, including the different projectiles that the
ships shoot at each other, will be fully developed.
The game will consist of one hero ship located at the bottom of the stage (see Figure 2-10) that can be moved horizontally using the mouse. Five alien ships are placed in a row at the top of the stage. The alien ships shoot alien cannon balls (unfilled circles) and alien mines (unfilled squares that rotate). Clicking the mouse will make the hero ship shoot hero cannon balls (filled circles). In this example, we will not implement collision detection (aka hit testing) to figure out if projectiles hit the ships.
Figure 2-10. Screenshot ...