
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
442
|
Chapter 19: The Delegation Event Model
debugging purposes, onRandomAction( ) also displays the time elapsed since the last
shooting star was created.
public function onRandomAction (e:RandomizerEvent):Void {
trace("New shooting star! Time since last star: " + e.getTimeSinceLast( ));
makeShootingStar( );
}
Reader exercise: try modifying the code so that the chance of a shooting star appear-
ing dynamically decreases or increases depending on the amount of time elapsed
since the last star. Hint—use
e.getSource( ) to access the Randomizer.restart( )
method.
Using NightSky in an Application
To use the NightSky class in an application, we create an instance of Randomizer and
an instance of NightSky, then we use addRandomizerListener( ) to register the
NightSky instance as a listener with the Randomizer instance. The following code
shows the technique:
import nightsky.*;
import util.*;
var sky:NightSky = new NightSky(bg_mc, 1, 0);
var starRandomizer:Randomizer = new Randomizer(1000, 3);
starRandomizer.addRandomizerListener(sky);
The NightSky instance (sky) creates a sky graphic in the movie clip bg_mc at depth 1.
Shooting stars in the sky will start appearing on depth 0. The Randomizer instance
(
starRandomizer) has a one-in-three chance of triggering onRandomAction( ) every
second (i.e., every ...