User Guide

Using classes and ActionScript 2.0 99
class mx.site.Utils {
static function randomRange(min:Number, max:Number):Number {
if (min>max) {
var temp:Number = min;
min = max;
max = temp;
}
return (Math.floor(Math.random()*(max-min+1))+min);
}
static function arrayMin(num_array:Array):Number {
if (num_array.length == 0) {
return Number.NaN;
}
num_array.sort(Array.NUMERIC | Array.DESCENDING);
var min:Number = Number(num_array.pop());
return min;
}
static function arrayMax(num_array:Array):Number {
if (num_array.length == 0) {
return undefined;
}
num_array.sort(Array.NUMERIC);
var max:Number = Number(num_array.pop());
return max;
}
}
You might use these functions by adding the following ActionScript to your FLA file:
import mx.site.Utils;
var randomMonth:Number = Utils.randomRange(0, 11);
var min:Number = Utils.arrayMin([3, 3, 5, 34, 2, 1, 1, -3]);
var max:Number = Utils.arrayMax([3, 3, 5, 34, 2, 1, 1, -3]);
trace("month: "+randomMonth);
trace("min: "+min);
trace("max: "+max);
Using classes and ActionScript 2.0
You create classes in separate ActionScript files that are imported into a SWF file when it is
compiled. To create a class file, the code you write can have a certain methodology and ordering,
which is discussed in the following sections.
Use the following basic ordering in your AS class files:
1.
Documentation comments
2.
Package and import statements
3.
Class and interface declarations