
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
48
|
Chapter 2: Variables
When you define a variable on a timeline, that variable is accessible
from all the other frames of that timeline.
Scenario 2: Accessing a value defined later on the same timeline
What happens if we try instead to access a variable before the frame in which it is
assigned a value?
Suppose we add the following code to frame 30 of a movie’s main timeline:
password = "let_me_in";
gotoAndStop(15);
and on frame 15 we add:
trace(password);
When the movie plays, we see the following in the Output window:
undefined
let_me_in
Here’s why: when the movie’s playhead reaches frame 15, Flash displays the value of
password in the Output window. The first time through, password does not exist, so
Flash displays
undefined. However, when the playhead reaches frame 30, password is
created and assigned the value “let_me_in”. Then the playhead is sent back to frame
15 (gotoAndStop(15)), causing frame 15’s code to execute again. This time, even
though
password is defined on a later frame than our trace(password) statement, it is
still part of the same timeline; so, its value exists, and Flash displays it in the Output
window.
Any variable declared on a timeline is available to all the scripts of its
timeline for as long as that timeline exists. However, a variable’s value
is not defined until ...