Vous êtes sur la page 1sur 6

Quick Reference

for Flash MX Programmers

ActionScript
for Flash MX
Pocket Reference

Colin Moock
ActionScript for Flash MX
Pocket Reference

Colin Moock

Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo


Variables
ActionScript variables can contain any type of data and can
be defined in one of three scopes: timeline, global, or local.

Timeline Variables
Timeline variables are variables declared with the var state-
ment on a frame of a timeline, or in a button’s on( ) handler
or a movie clip’s onClipEvent( ) handler. Once defined, time-
line variables are accessible directly from any other frame on
the same timeline, and indirectly via dot syntax from other
movie clip timelines. For example:
// Declare timeline variable length
var length = 10;

// Assign a value to variable seenAlready


// in a nested movie clip, intro_mc
intro_mc.seenAlready = true;

To access timeline variables defined on the movie clip that


contains the current movie clip, use _parent:
// Set velocity in the containing clip.
_parent.velocity = 10;

// Set language in the clip that contains


// the containing clip (two levels up
// the clip hierarchy).
_parent._parent.language = "English";

To access variables defined on a movie’s main timeline, use


_root or _leveln, where n is a document level in the Flash
Player. For example:
// Set x on the main timeline of the
// current movie.
_root.x = 10;

// Set y on the main timeline of the .swf


// file loaded into level 1.
_level1.y = 15;

Variables | 13
Note, however, that if movie1.swf is loaded into a movie clip
in movie2.swf, the _root property in movie1.swf refers to the
main timeline of movie2.swf! To create an unchanging refer-
ence to a movie’s main timeline, add the following code to
frame 1 of the main timeline, substituting your movie’s name
for movieName:
_global.movieNameMainTimeline = this;

Thereafter, from any timeline in movieName, use


movieNameMainTimeline in place of _root.

Global Variables
Global variables are accessible from any timeline throughout
any movie currently loaded into the Flash Player. To create a
global variable, or to assign a global variable a new value,
use:
_global.varName = value;

To read a global variable’s value, simply refer to varName


directly. For example:
// Create global variable numPlayers.
_global.numPlayers = 2;

// Pass numPlayers to startGame() function.


startGame(numPlayers);

When a timeline variable and a global variable have the same


name, the timeline variable takes precedence (it shadows the
global variable). To access a shadowed global variable,
include _global before the variable name. For example:
_global.name = "Colin"; // Global variable
var name = "Bruce"; // Timeline variable
trace(name); // Displays: Bruce
trace(_global.name); // Displays: Colin

14 | ActionScript for Flash MX Pocket Reference


Local Variables
Local variables are created with the var statement in the body
of a function. They are accessible within the function only,
and they expire when the function finishes executing. For
example, the following function uses two local variables,
time and hour, in the calculation of the current hour:
// Returns the current hour, in
// 12-hour clock format.
function getHours12 () {
var time = new Date();
var hour = time.getHours();
if (hour > 12) {
hour -= 12;
} else if (hour == 0) {
hour = 12;
}
return hour;
}

Code Hinting
Table 3 lists the variable name suffixes that cause the Actions
panel to display a quick-reference pop-up (so-called code
hinting) when the variable name is entered. For example, to
enable code hinting for a text field variable, you can name
the variable output_txt.

Table 3. Code-hinting suffixes


Suffix Datatype or class represented
_mc MovieClip
_array Array
_str String
_btn Button
_txt TextField
_fmt TextFormat
_date Date

Variables | 15
Table 3. Code-hinting suffixes (continued)
Suffix Datatype or class represented
_sound Sound
_xml XML
_xmlsocket XMLSocket
_color Color
_video Video
_ch FCheckBox
_pb FPushButton
_rb FRadioButton
_lb FListBox
_sb FScrollBar
_cb FComboBox
_sp FScrollPane

16 | ActionScript for Flash MX Pocket Reference

Vous aimerez peut-être aussi