
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
432
|
Chapter 19: The Delegation Event Model
NightSky: A Delegation Event
Model Example
Now that we have established a foundation for delegation event model implementa-
tions, let’s use it in a real example: a starry night sky with animated shooting stars.
The code discussed in this section is available at http://moock.org/eas2/examples. This
example breaks from earlier examples in this book, which focused primarily on appli-
cation development. Here, we see how OOP and design patterns can be applied to
motion graphics as well as application development. Of course, a random event gener-
ator is also perfect for video games, which make heavy use of OOP.
The Event Source
The event source for our example is Randomizer, a class that triggers an event named
onRandomAction( ) at random intervals. Every n seconds, the onRandomAction( )
event has a user-defined chance of occurring. For example, a specific Randomizer
instance might, every 5 seconds, have a 1-in-20 chance of triggering
onRandomAction( ).
The onRandomAction( ) event typically activates some random behavior in
Randomizer’s listeners, such as the blinking of a cartoon character’s eyes, the spawn-
ing of a random monster in a game, or, as in our case, the appearance of a shooting
star animation in a night sky. The Randomizer class is a ...