< Day Day Up >
Creating Variables
You have created many variables in Lessons 15. Here in Lesson 6, we formally
introduce variables and how they are created.
Each timeline in your project can contain its own set of variables—containers, so to
speak, for storing information (data). When you create variables, you must give them
names—monikers you cannot change, though you can alter their contents. Here's the
syntax for creating a variable:
var myFullName:String = "Jobe Makar";
The script declares a new variable, associates a data type with that variable, and gives the
variable data to store.
By using var followed by a space, you indicate that the next string of letters is the
declaration of a variable. After the variable name (the string of letters after var) you add a
colon. The Actions panel gives you a drop-down list of data types you can add after the
colon, or you can type the data type yourself. The assignment operator (=) sets what is
found to its right as the value of the variable. Above, "Jobe Makar" is the value of
myFullName.
N
OTE
As you learned in Lesson 1, "Introducing ActionScript," by associating a data type with a
variable you allow the Flash player to assist you in debugging scripts. If you declare a
variable as a string but later attempt to assign a number as a value, the Flash player will
catch this error and report it to you when you export your movie (at "compile time").
You can also declare a variable without giving it a value. For example:
var myFullName:String;
The script tells the Flash Player that when the variable myFullName is used at some point
in the future, it should have a String value.
It is also possible to create a variable this way:
myFullName = "Jobe Makar";
This script is a valid way to create a variable. However, creating a variable in this manner
is no longer recommended. It is now recommended to use the var syntax so that you can
make full use of data typing and for good memory management.
N
OTE
You can name a variable anything, as long as you follow some simple rules. The name
must begin with a character or underscore, and it cannot contain spaces, special
characters (@, #, $, %, and so on), or punctuation marks. Even though you might not
receive an error from Flash if you use a special character for a variable name, it can still
cause unplanned behavior.
TIP
N
ame your variables according to the data they contain—for example, numberOfDays,
favoriteDogsName, totalTax, and so on—so you can remember and use them easily
throughout your project.
Once you create a variable and assign it a value, you can use that value in any script
simply by referencing its name. For example:
var myFavoriteNumber:Number = 66;
This code creates a variable named myFavoriteNumber and assigns it a value of 66. To
use that value in a script, you use syntax like this:
myButton_btn.onRelease = function() {
gotoAndStop (myFavoriteNumber); // Moves the timeline to frame 66
cat_mc._xscale = myFavoriteNumber; // Scale cat movie clip instance 66% vertically
};
Variables derive their power from universally available data—that is, information that
can be used in any script. If the value of your variable changes, the scripts that use it will
execute differently because some of the actions are based on the current value of that
variable.
The three main types of variable values are String, Boolean, and Number. A String is a
text value. For example, var myFullName:String = "Jobe Makar" has the String value
"Jobe Makar". Strings are most often used to store text, such as in the example var
latestNews:String = "We just got a new dog!" String values are defined using quotation
marks (""). The syntax var myFavoriteNumber:String = "27" assigns a text value of "27"
to myFavoriteNumber, not a number value of 27.
A Boolean value is a true or false value such as the following:
var playMusic:Boolean = true;
In programming, Boolean values are often used to indicate whether something is on or
off: A script can look at the Boolean's current state (on or off, true or false) and act
accordingly. For example, if you were to create a music toggle button for a Flash movie,
you might want to set a Boolean variable to store the music's state—on or off (var
musicPlaying:Boolean = true or var musicPlaying:Boolean = false). You could attach a
script to the button so that when clicked, it would check to see if the music was on or off
(true or false). If the music were currently on (true), the idea would be to turn the music
off and then switch the value of the variable to false. If the music were off (false), the
music would need to be turned on and the value of the variable set to true. Because the
value of musicPlaying is switched between true and false with each successive button
click, the script on the button would evaluate its current value and turn the music on or
off.
N
OTE
In Lesson 8, "Using Conditional Logic," we'll cover Boolean values and their uses in
more depth.
A Number value is just that, a number. Numbers are used to store numeric values—often
for mathematical use. You can use numeric values for
p
eople's ages, for scores in a game,
and to track the number of times someone has clicked a button—to name just a few uses.
Here's how you would create a variable and assign a number value to it:
var radius:Number = 32;
Instead of assigning direct values (or literals)—for example, 32, "dog," or something
else—to variables, you can use an expression to set the value. An expression is a
phrase—or a collection of variables, numbers, text, and operators—that evaluates to a
string, number, or Boolean value. For example:
var bottlesOfBeerOnTheWall:Number = 99;
var oneFellDown:Number = 1;
var bottlesLeft:Number = bottlesOfBeerOnTheWall - oneFellDown;
The third line in this script uses an expression to set the value of bottlesLeft. The
expression substitutes the values of the variables (99 and 1) and performs a subtraction.
The end result is that the variable bottlesLeft is assigned a value of 98. It's important to
note that the structure of the expression determines whether it will result in a string,
Boolean, or number value.
When using expressions to set variable values, the expression doesn't have to be lengthy.
Take a look at this example:
var someone:String = "Jobe Makar";
var coffeeBoy:String = someone;
A variable called someone is created in the first line of the script. In the second line, the
value of the coffeeBoy variable is set to the string "Jobe Makar" by referencing the value
of someone. If the value of someone changes later, coffeeBoy will not reflect the change.
N
OTE
A variable can also gain its value as the result of a function. For more on this topic, see
Lesson 5, "Using Functions."
A Flash project can contain many timelines, each with its own set of dynamic data
(variables, arrays, and so on). The timeline where dynamic data resides depends on the
timeline on which it was placed when it was created. This script creates a variable named
myVariable in the current movie:
var myVariable:Number = 100;
The next script creates a variable named myVariable and places it on the root (main)
timeline:
_
root.myVariable = 100;
N
otice the use of a target path in this variable's creation. Using target paths in this
manner, you create or access data on a specific timeline.
N
ote that using a target path to create a variable restricts you from using the