By Colin Moock
Cover | Table of Contents | Colophon
ball_one bounce around the screen."
http://www.moock.org/webdesign/flash/contact.html.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: The Definitive Guide";
bookTitle is the variable's
name (its identifier). On
the right side of the equal sign, the phrase "ActionScript: The
Definitive Guide" is the variable's value
—the datum you're depositing. The equal sign
itself is called the assignment operator. It
tells Flash that you want to assign (i.e., deposit) whatever is on
the right of the equal 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 implicitly create variables
isn't recommended).speed = 25; output = "thank you";
speed, showing that variables can contain numbers
as well as text. We'll see shortly that they 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 the value of the expression 1 + 5:y = 1 + 5;
y = 1 + 5; is executed, 1 is
first added to 5, yielding 6, and then 6 is assigned to
y. The expression on the right side of the equal
sign is evaluated (calculated or resolved)
before setting the variable on the left side equal to that result.
Here we assign an expression that contains the variable
y to another variable, z: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 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.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 the
._x indicates its x-coordinate property (i.e.,
horizontal position on stage). We'll learn more about
properties later. The last line, trace(firstName),
displays a variable's value while a script is running, which is
handy for debugging your code.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);
x as we did in Scenario
1, but instead of placing the variable-setting code on frame 1
directly, we place it on a button in frame 1. Then, on frame 2, we
attach the same code as before:trace(x);
x is
attached to our button, and our button is attached to the main
timeline, our variable is indirectly attached to the main timeline.
We may, therefore, access the variable from frame 2 as we did before.secretPassword
on frame 1 of the main timeline. When the movie plays, the user must
guess the password in order to gain access to a special section of
the movie.secretPassword on frame
1, we create a function that compares the user's guess to the
real password. Here's our code:var randomFrame; // Stores the randomly picked frame number
var numFrames; // Stores the total number of frames on the timeline
numFrames = _totalframes; // Assign _totalframes property to numFrames
// Pick a random frame
randomFrame = Math.floor(Math.random( ) * numFrames + 1);
gotoAndStop(randomFrame); // Send playhead 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 root of (deltaX squared plus deltaY squared). dist = Math.sqrt((deltaX * deltaX) + (deltaY * deltaY)); // Tidy references are much more readable than the alternative: 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 later. Before we think about making our own data
categories, let's see which categories come built into
ActionScript.hi
there," ActionScript provides the
string
datatype. 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
1999 + 1 // Yields the datum 2000 "hi " + "ma!" // Yields the 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:// This datum is fleeting, and dies immediately after it's created "hi " + "ma"; // This datum is stored in a variable and can be // accessed later via the variable
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 internal step):999 - NaN;
999
-
NaN yields the value
NaN, which is the final value of our
expression.var custName = "Derek"; var custTitle = "Coding Genius"; var custAge = 30; var custPhone = "416-222-3333";
cust1Name,
cust2Name, cust1Title,
cust2Title, and so on. Yuck! But if we use an
array, we can store our information much more efficiently:Return to ActionScript: The Definitive Guide