By Colin Moock
Book Price: $54.95 USD
£38.95 GBP
PDF Price: $43.99
Cover | Table of Contents | Colophon
ball_one bounce around the
screen."
http://www.moock.org/contact/.
undefined (indicating the absence of
data).
var speed; var bookTitle; var x;
speed,
bookTitle, or x, becomes our
new variable's name. We can create variables
anywhere we can attach code: on a keyframe, a button, or a movie
clip.
var x, y, z;
variableName = value;
bookTitle = "ActionScript for Flash MX: The Definitive Guide";
bookTitle is the variable's
name (its identifier). On
the right side of the equals sign, the phrase
"ActionScript for Flash MX: The Definitive
Guide" is the variable's
value—the datum you're
depositing. The equals sign is called the
assignment operator. It tells Flash that you
want to assign (i.e., deposit) whatever is on the right of the equals
sign to the variable shown on the left. If the variable on the left
doesn't exist yet, Flash creates it (though relying
on the interpreter to create variables implicitly
isn't recommended).
speed = 25; output = "thank you";
speed, showing that variables can contain numbers
as well as text. We'll see shortly that variables
can contain other kinds of data as well. The second example assigns
the text "thank you" to the
variable output. Notice that we use straight
double quotation marks (" ") to delimit a text string in
ActionScript.
y:
y = 1 + 5;
y. The expression on the right side of the equals
sign is evaluated (calculated or resolved)
before assigning the result to the variable on the left side. Here,
we assign an expression that contains the variable
y to another variable, var firstName; // Declare the variable firstName firstName = "Graham"; // Set the value of firstName firstName = "Gillian"; // Change the value of firstName firstName = "Jessica"; // Change firstName again firstName = "James"; // Change firstName again var x = 10; // Declare x and assign it a numeric value x = "loading...please wait..."; // Assign x a text value
x's datatype
from numeric to text data by simply assigning it a value of the
desired type. Some programming languages don't allow
the datatype of a variable to change, but ActionScript does.
_x is a built-in
property representing a movie clip's horizontal
position with no relation to the variable named x
used in preceding examples (see the "Variable Naming
Styles" sidebar in Chapter 1, and
see MovieClip._x in the Language
Reference).
newX = oldX + 5; // Set newX to the value of oldX plus 5 ball._x = newX; // Set the horizontal position of the // ball movie clip to the value of newX trace(firstName); // Display the value of firstName in the Output window
ball._x,
ball is a movie clip's name, and
._x indicates its x-coordinate property (i.e.,
horizontal position on stage). We'll learn more
about properties later. The last line,
x = 1; // x is a number x = "Michael"; // x is now a string x = [4, 6, "hello"]; // x is now an array x = 2; // x is a number again
x, in frame 1 of the
main timeline. After creating x, we set its value
to 10:
var x; x = 10;
trace(x);
password = "let_me_in"; gotoAndStop(15);
trace(password);
movieObj.SetVariable("/someClip:firstName", "Colin");
http://www.moock.org/webdesign/flash/fscommand/
<OBJECT> and
<EMBED> tags can create variables in a Flash
movie, when the movie initially loads, via the
FlashVars parameter or a query string. Variables
passed to a Flash movie via FlashVars or a query
string must be URL-encoded according to the rules described in the
LoadVars class in the Language
Reference. The following code uses the
FlashVars parameter to create the variables
var randomFrame; // Stores the randomly picked frame number
var numFrames; // Stores the total number of frames in the clip
numFrames = this._totalframes; // Store the current movie clip's
// _totalframes property in numFrames
// Pick a random frame
randomFrame = Math.floor(Math.random( ) * numFrames + 1);
this.gotoAndStop(randomFrame); // Send playhead of current movie clip to
// chosen random frame
var c; // A convenient reference to the circle clip object var s; // A convenient reference to the square clip object var deltaX; // The horizontal distance between c and s var deltaY; // The vertical distance between c and s var dist; // The total distance between c and s c = _root.circle; // Get reference to the circle clip s = _root.square; // Get reference to the square clip deltaX = c._x - s._x; // Compute the horizontal distance between the clips deltaY = c._y - s._y; // Compute the vertical distance between the clips // The distance is the square root of (deltaX squared plus deltaY squared). dist = Math.sqrt((deltaX * deltaX) + (deltaY * deltaY)); // Using variables, the code above creates tidy references that // are much more readable than the alternative, shown below: dist = Math.sqrt(((_root.circle._x - _root.square._x) * (_root.circle._x - _root.square._x)) + ((_root.circle._y - _root.square._y) * (_root.circle._y - _root.square._y)));
010101010101010110101011011010101010101010000010101010101011010101010 101010101010101011101010101010101010101010101010111110101010101010101 010101010101010101010101011101010101010101010101010101010101010101010
010101010101010110101011011010101010101010000010101010101011010101010 101010101010101011101010101010101010101010101010111110101010101010101 010101010101010101010101011101010101010101010101010101010101010101010
phoneNumber and faxNumber. In
more complex situations, we can create our own custom data categories
with objects and object
classes, as covered in Chapter 12.
Before we think about making our own data categories,
let's see which categories come built into
ActionScript.
hi
there". A
string is a series of characters (alphanumerics
and punctuation).
"loading...please wait" // A string literal
1.51 // A numeric literal
["jane", "jonathan"] // An array literal
{x: 10, y: 15} // An object literal
1999 + 1 // Yields the numeric datum 2000 "1999" + "1" // Yields the string datum "19991" "hi " + "ma!" // Yields the string datum "hi ma!" firstName // Yields the value of the variable firstName _currentframe // Yields the frame number of the playhead's current position new Date() // Yields a new Date object with the current date and time
"hi" + "ma!"
is lost unless we store it, say, in a variable. For example:
999 - "Flash";
NaN
(i.e., Not-a-Number).
NaN is a legal value of the
number datatype, intended specifically to handle
such a situation. With "Flash"
converted to NaN, our expression ends up looking
like this to the interpreter (though we never see this interim step):
999 - NaN;
999
-
NaN yields the value NaN, which
is the final value of our expression.
var custName = "Derek";
cust1Name,
cust2Name, cust3Name, and so
on. Yuck! But if we use an array, we can store our information much
more efficiently:
x to variable
y), compare it (e.g., check
whether x equals y), and
pass it (e.g., supply a variable to a function
as an argument). Primitive data values are copied, compared, and
passed quite differently than composite data. When primitive data is
copied to a variable, that variable gets its own unique and private
copy of the data, stored separately in memory. Hence, the following
lines of code cause the string
"Dave" to be stored twice in
memory, once in the memory location reserved for
name1 and again in the location reserved for
name2:
name1 = "Dave"; name2 = name1;
MIN_VALUE and