The ActionScript editor supports code hints for built-in classes and for variables that are based
on these classes. If you want Flash to provide code hints for a particular object type that you
assign to a variable, you can strictly type the variable. Code hints provide tooltip-style syntax
hints and a pop-up menu that helps you write your code quickly.
For example, type the following code:
var members:Array = new Array();
members.
As soon as you type the period (
properties available for Array objects.
For recommended coding conventions for naming variables, see
on page
736.
Using variables in an application
In this section, you use variables in short code snippets of ActionScript. You need to declare
and initialize a variable in a script before you can use it in an expression. Expressions are
combinations of operands and operators that represent a value. For example, in the expression
,
and
are operands, and
i+2
i
2
If you do not initialize a variable before you use it in an expression, the variable is undefined
and may cause unexpected results. For more information on writing expressions, see
5, "Syntax and Language Fundamentals," on page
If you use an undefined variable, as shown in the following example, the variable's value in
Flash Player 7 and later will be
var squared:Number = myNum * myNum;
trace(squared); // NaN
var myNum:Number = 6;
In the following example, the statement that declares and initializes the variable
first, so
can be replaced with a value:
squared
var myNum:Number = 6;
var squared:Number = myNum * myNum;
trace(squared); // 36
Similar behavior occurs when you pass an undefined variable to a method or function, as
shown next.
To compare undefined and defined variables being passed to a function:
1.
Drag a Button component to the Stage from the Components panel.
2.
Open the Property inspector and type bad_button into the Instance Name text box.
92
Data and Data Types
) in the Actions panel, Flash displays a list of methods and
.
is an operator.
+
113.
, and your script might produce unintended results:
NaN
"Naming variables"
Chapter
myNum
comes
Need help?
Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?