May 2001
Intermediate to advanced
720 pages
23h 24m
English
Math.max( ) Method — determine the larger of two numbers
Flash 5; may be used when exporting Flash 4 movies
Math.max(x, y)
A number.
A number.
The larger of x and
y.
Math.max(5, 1); // Returns 5 Math.max(-6, -5); // Returns -5
This example constrains a value to the specified range:
function constrainToRange (checkVal, minVal, maxVal) {
return Math.min(Math.max(checkVal, minVal), maxVal);
}
// Constrain the slider to the stage area
mySlider._x = constainToRange (mySlider._x, 0, 550);This example returns the maximum value in an array:
function maxInArray (checkArray) {
maxVal = -Number.MAX_VALUE; // Initialize maxVal to a very small number
for (var i = 0; i < checkArray.length; i++) {
maxVal = Math.max(checkArray[i], maxVal);
}
return maxVal;
}
trace(maxInArray([2,3,66,4,342,-90,0])); // Displays: 342
Math.min( )
Read now
Unlock full access