136
Syntax and Language Fundamentals
For more information on objects and properties, see
“Object data type” on page 78
. For a list
of constants in the language (such as
false
and
NaN
), see the ActionScript Language Elements
> Constants category in the
ActionScript 2.0 Language Reference
.
Using constants
Constants are properties with a fixed value that cannot be altered; in other words, they are
values that don’t change throughout an application. The ActionScript language contains many
predefined constants. For example, the constants
BACKSPACE
,
ENTER
,
SPACE
, and
TAB
are
properties of the Key class and refer to keyboard keys. The constant
Key.TAB
always has the
same meaning: it indicates the Tab key on a keyboard. Constants are useful for comparing
values and for using values in your application that do not change.
To test whether the user is pressing the Enter key, you could use the following statement:
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
if (Key.getCode() == Key.ENTER) {
trace("Are you ready to play?");
}
};
Key.addListener(keyListener);
For the previous ActionScript to work, it may be necessary to disable keyboard shortcuts in
the authoring environment. Select Control > Test Movie from the main menu, then while
previewing the SWF file in the player, select Control > Disable Keyboard Shortcuts from the
SWF file’s preview window.
In Flash there is no way to create your own constant values except when you create your own
custom classes with private member variables. You cannot create a “read-only” variable
within Flash.
Variables should be lowercase or mixed-case letters; however, constants (variables that do not
change) should be uppercase. Separate words with underscores, as the following ActionScript
shows:
var BASE_URL:String = "http://www.macromedia.com"; //constant
var MAX_WIDTH:Number = 10; //constant
Write static constants in uppercase, and separate words with an underscore. Do not directly
code numerical constants unless the constant is 1, 0, or -1, which you might use in a
for
loop
as a counter value.
You can use constants for situations in which you need to refer to a property whose value
never changes. This helps you find typographical mistakes in your code that you might not
find if you use literals. It also lets you change the value in a single place. For more information
on literals, see
“About literals” on page 130
.
Содержание FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Страница 1: ...Learning ActionScript 2 0 in Flash...
Страница 8: ...8 Contents...
Страница 18: ...18 Introduction...
Страница 30: ...30 What s New in Flash 8 ActionScript...
Страница 66: ...66 Writing and Editing ActionScript 2 0...
Страница 328: ...328 Interfaces...
Страница 350: ...350 Handling Events...
Страница 590: ...590 Creating Interaction with ActionScript...
Страница 710: ...710 Understanding Security...
Страница 730: ...730 Debugging Applications...
Страница 780: ...780 Deprecated Flash 4 operators...
Страница 830: ...830 Index...