# AS3 random events using a timer

I'm making a game for an assignment at university. The game I am making requires random household appliances around a house to "turn on" at random times. The player must run around the house turning this off.

I am confident with the rest of the code for the game, but the random turning on is what I'm not sure on.

I am thinking the solution would be to use an array with all my appliances pushed into into, then create a timer event. From this a random time would be picked from the timer, and then a random appliance would be picked from the array. The appliance on animation would then be displayed until the player clicked on it to turn it off.

Only problem is i'm not sure how to start this.

Also worth mentioning, I am only able to use flashdevelop and AS3 for this project, so no Adobe Flash.

Job4aCowboy

Before you read the code provided make sure you understand the pseudo code.

// Assumptions:
// Appliance objects provide the turnOn() function.
// We have a collection of appliances we can access somehow.

Pseudocode:

// Constructor
// Gather our appliances...
// Kickoff the randomized start function

// Randomized Start Function
// Start a random appliance
// Select an appliance at random
// Get a random number
// Use that number as the selected appliance
// Start the selected appliance
// Call this function again after a random delay
// Get a random number
// Use that number as the random delay
// Reset the timer to 0
// Set the timer to 'go-off' after the random delay
// Set the timer to call this function when it goes off
// Start the timer
// END

var static const MAX_DELAY:uint = 10;
var static const MIN_DELAY:uint = 1;

var appliances:Array = new Array();
var timer:Timer = new Timer(0, 1);

// Constructor
public function ApplianceGhost():void{
// Gather our appliances... from somewhere?
for (var cur:Appliance in applianceCollection) {
appliances.push(cur);
}

startApplianceAndLoop();
}

function startApplianceAndRecurse():void {
// Start a random appliance
startRandomAppliance();

// Call this function again after a random delay
var randomDelay:Number = randomFromRange(MIN_DELAY, MAX_DELAY);
timer.reset();
timer.delay = randomDelay
timer.start();
}

function startRandomAppliance():void {
// Select an appliance at random
var applianceToStart:int = randomFromRange(0, appliances.length);

// Start the selected appliance
appliances[applianceToStart].turnOn();
}

// Return n such that:   min <= n <= max
public function randomFromRange(min:Number, max:Number):Number {
return (Math.floor(Math.random() * (max - min + 1)) + min);
}


Disclaimer:

1. I wrote this code in Sublime Text so it hasn't been exposed to autocomplete or a compiler.
2. I probably won't offer suggestions on syntax errors or 'A won't compile because X' (practice solving these; they are a surprisingly large part of being a Software Engineer).
3. This algorithm starts the first appliance immediately, you might need to start the first appliance after a delay.
4. Random from range is borrowed from here.