- a Randomizer class that generates random timed events
- can be used for things like:
- random character eye blinking
- random monster spawning
- random shooting stars (we'll examine this example)
- Randomizer is the event source
- RandomizerEvent is the event object
- RandomizerListener is the interface
- NightSky is the event listener

// PACKAGE
AsSetupPackage("org.moock.util");
// CLASS Randomizer
/**
* Generates random events.
*
* @param interval The time in milliseconds to wait between
* checks for a random event.
* @param odds The likelihood an event will be triggered at
* each check. An event has a 1 in odds chance of
* being triggered.
*/
org.moock.util.Randomizer = function (interval, odds) {
// Initialize the time of the most recent event.
this.lastEventTime = new Date();
// Start checking for events.
this.start(interval, odds);
// Create an EventListenerList to manage listeners.
this.listenerList = new org.moock.event.EventListenerList();
}
// PROPERTIES
// The event listeners that will receive events.
org.moock.util.Randomizer.prototype.listenerList = null;
// The time in milliseconds between checks for a random event.
org.moock.util.Randomizer.prototype.randInterval = null;
// The time of the last random event.
org.moock.util.Randomizer.prototype.lastEventTime = null;
// METHODS
/**
* Register a RandomizerListener object to receive random events.
*/
org.moock.util.Randomizer.prototype.addRandomizerListener = function (l) {
return this.listenerList.addObj(l);
}
/**
* Unregister an event listener object.
*/
org.moock.util.Randomizer.prototype.removeRandomizerListener = function (l) {
return this.listenerList.removeObj(l);
}
/**
* Start an interval to check for random events.
*
* @param interval The time in milliseconds to wait between
* checks for a random event.
* @param odds The likelihood an event will be triggered at
* each check. An event has a 1 in odds chance of
* being triggered.
*/
org.moock.util.Randomizer.prototype.start = function (interval, odds) {
if (interval > 0 && odds > 1) {
// Call this.check() every interval milliseconds.
this.randInterval = setInterval(this, "check", interval, odds);
}
}
/**
* Stop checking for random events.
*/
org.moock.util.Randomizer.prototype.stop = function () {
clearInterval(this.randInterval);
}
/**
* Restart the event-checking interval, possibly with new odds.
*
* @param interval The time in milliseconds to wait between
* checks for a random event.
* @param odds The likelihood an event will be triggered at
* each check. An event has a 1 in odds chance of
* being triggered.
*/
org.moock.util.Randomizer.prototype.restart = function (interval, odds) {
this.stop();
this.start(interval, odds);
}
/**
* Checks to see if a random event occurs, based on the
* current odds.
*
* @param odds The likelihood an event will be triggered at
* each check. An event has a 1 in odds chance of
* being triggered.
*/
org.moock.util.Randomizer.prototype.check = function (odds) {
// Local variables.
var rand = Math.floor(Math.random() * odds);
var now = new Date(); // Current time
var elapsed; // Time since last event
// If the random event occurs...
if (rand == 0) {
// Determine the elapsed time since the last event.
elapsed = now.getTime() - this.lastEventTime.getTime();
this.lastEventTime = now;
// Fire the event.
this.fireOnRandomAction (elapsed);
}
}
/**
* Invokes onRandomAction on all listeners.
*
* @param elapsed The amount of time since the last event notification.
*/
org.moock.util.Randomizer.prototype.fireOnRandomAction
= function (elapsed) {
// Get a list of the current event listeners.
var listeners = this.listenerList.getListeners();
// Create an object to describe the event.
var e = new org.moock.util.RandomizerEvent(this, elapsed);
// Broadcast the event to all listeners.
for (var i = 0; i < listeners.length; i++) {
listeners[i].onRandomAction(e);
}
}
- RandomizerEvent source code:
// PACKAGE
AsSetupPackage("org.moock.util");
// CLASS RandomizerEvent EXTENDS org.moock.event.EventObject
/**
* An event object describing a Randomzier event.
*
* @param src The event source (a Randomizer instance).
* @param timeSinceLast The number of milliseconds since the last event.
*/
org.moock.util.RandomizerEvent = function (src, timeSinceLast) {
// Always invoke superclass constructor!!
super(src);
// Record the time since the last event.
this.timeSinceLast = timeSinceLast;
}
// SET SUPERCLASS
org.moock.util.RandomizerEvent.prototype = new org.moock.event.EventObject();
// PROPERTIES
// The number of milliseconds since the last event
// was broadcast.
org.moock.util.RandomizerEvent.prototype.timeSinceLast = null;
// METHODS
/**
* Returns the time since the last event.
*/
org.moock.util.RandomizerEvent.prototype.getTimeSinceLast = function () {
return this.timeSinceLast;
}
- RandomizerListener source code:
/**
* interface RandomizerListener EXTENDS org.moock.event.EventListener
*
* Lists the methods that must be implemented by
* an object that wants to be notified of Randomzier events.
*/
/**
* public function onRandomAction (e)
*
* Triggered when a random event has occured (according to the
* odds and interval set for the Randomizer).
*
* @param e A RandomizerEvent instance.
*/
// PACKAGE
AsSetupPackage("org.moock.examples.nightsky");
// CLASS NightSky IMPLEMENTS org.moock.util.RandomizerListener
/**
* Creates a sky full of stars, and listens for Randomizer
* events to create random shooting stars.
*
* @param target The MovieClip in which to create the sky.
* @param skyDepth The depth at which to create the sky in target.
* @param starDepth The depth at which to start creating shooting
* stars in the sky clip.
*/
org.moock.examples.nightsky.NightSky = function (target, skyDepth, starDepth) {
this.target = target;
this.skyDepth = skyDepth;
this.starDepth = starDepth;
this.makeSkyBG();
}
// PROPERTIES
// The MovieClip in which to create the sky.
org.moock.examples.nightsky.NightSky.prototype.target = null;
// The sky movie clip.
org.moock.examples.nightsky.NightSky.prototype.sky_mc = null;
// The depth on which to create the sky. Defauts to 0.
org.moock.examples.nightsky.NightSky.prototype.skyDepth = 0;
// The depth at which to create shooting stars. Defaults to 0.
org.moock.examples.nightsky.NightSky.prototype.starDepth = 0;
// METHODS
/**
* Responds to random events from a Randomizer object.
* Creates a shooting star in the sky.
*
* @param e An object that describes the event.
*/
org.moock.examples.nightsky.NightSky.prototype.onRandomAction
= function (e) {
trace("New shooting star! Time since last star: " + e.getTimeSinceLast());
this.makeShootingStar();
}
/**
* Creates a sky graphic by attaching a movie clip with the
* linkage identifier of "skybg".
*/
org.moock.examples.nightsky.NightSky.prototype.makeSkyBG = function () {
this.sky_mc = this.target.attachMovie("skybg", "skybg", this.skyDepth);
}
/**
* Creates a shooting star by attaching a movie clip with the
* linkage identifier of "shootingstar".
*/
org.moock.examples.nightsky.NightSky.prototype.makeShootingStar
= function () {
// Create the shooting star in the sky movie clip.
this.sky_mc.attachMovie("shootingstar",
"shootingstar" + this.starDepth,
this.starDepth);
// Randomly position the shooting star.
this.sky_mc["shootingstar" + this.starDepth]._x =
Math.floor(Math.random()
* this.target.skybg._width);
this.sky_mc["shootingstar" + this.starDepth]._y =
Math.floor(Math.random()
* this.target.skybg._height);
// Put the next shooting star on a higher depth.
this.starDepth++;
}
- example usage (in NightSky.fla):
#include "../org/moock/lang/AsSetupPackage.as"
#include "../org/moock/event/EventListenerList.as"
#include "../org/moock/event/EventObject.as"
#include "../org/moock/util/Randomizer.as"
#include "../org/moock/util/RandomizerEvent.as"
#include "../org/moock/examples/nightsky/NightSky.as"
var sky = new org.moock.examples.nightsky.NightSky(this, 0, 0);
var starRandomizer = new org.moock.util.Randomizer(1000, 3);
starRandomizer.addRandomizerListener(sky);