20
Chapter 1: ActionScript Basics
You can also use methods of the built-in Math and Number classes to manipulate numbers. For
more information on the methods and properties of these classes, see the
“Math class”
and
“Number class”
entries.
The following example uses the
sqrt()
(square root) method of the Math class to return the
square root of the number 100:
Math.sqrt(100);
The following example traces a random integer between 10 and 17 (inclusive):
var bottles:Number = 0;
bottles = 10 + Math.floor(Math.random()*7);
trace("There are " + b " bottles");
The following example finds the percent of the
intro_mc
movie clip loaded and represents it as
an integer:
var percentLoaded:Number = Math.round((intro_mc.getBytesLoaded()/
intro_mc.getBytesTotal())*100);
Boolean data type
A Boolean value is one that is either
true
or
false
. ActionScript also converts the values
true
and
false
to 1 and 0 when appropriate. Boolean values are most often used with logical
operators in ActionScript statements that make comparisons to control the flow of a script.
The following example checks that users enter values into two TextInput component instances.
Two Boolean variables are created,
userNameEntered
and
isPasswordCorrect
, and if both
variables evaluate to
true
, a welcome message is assigned to the
titleMessage
String variable.
//Add two TextInput components and one Button component on the Stage
//Strict data type the three component instances
var userName_ti:mx.controls.TextInput;
var password_ti:mx.controls.TextInput;
var submit_button:mx.controls.Button;
//Create a listener object, which is used with the Button component
//When the Button is clicked, checks for a user name and password
var btnListener:Object = new Object();
btnListener.click = function(evt:Object) {
//checks that the user enters at least one character in the TextInput
//instances and returns a Boolean true/false.
var userNameEntered:Boolean = (userName_ti.text.length>0);
var isPasswordCorrect:Boolean = (password_ti.text == "vertigo");
if (userNameEntered && isPasswordCorrect) {
var titleMessage:String = "Welcome "+userName_ti.text+"!";
}
};
submit_button.addEventListener("click", btnListener);
For more information, see
“Using built-in functions” on page 41
and
“Logical operators”
on page 36
.
Summary of Contents for FLEX
Page 1: ...Flex ActionScript Language Reference ...
Page 8: ......
Page 66: ...66 Chapter 2 Creating Custom Classes with ActionScript 2 0 ...
Page 76: ......
Page 133: ...break 133 See also for for in do while while switch case continue throw try catch finally ...
Page 135: ...case 135 See also break default strict equality switch ...
Page 146: ...146 Chapter 5 ActionScript Core Language Elements See also break continue while ...
Page 808: ...808 Chapter 7 ActionScript for Flash ...
Page 810: ...810 Appendix A Deprecated Flash 4 operators ...
Page 815: ...Other keys 815 Num Lock 144 186 187 _ 189 191 192 219 220 221 222 Key Key code ...
Page 816: ...816 Appendix B Keyboard Keys and Key Code Values ...
Page 822: ...822 Index ...