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 variablescontainers, so to speak, for storing information (data). When you create variables, you must give them namesmonikers you cannot change, though you can alter their contents. Here's the syntax for creating a variable:
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.
var myFullName:String = "Jobe Makar";
NOTEAs 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:
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:
var myFullName:String;
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.
myFullName = "Jobe Makar";
NOTEYou 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.
TIPName your variables according to the data they containfor example, numberOfDays, favoriteDogsName, totalTax, and so onso 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:
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:
var myFavoriteNumber:Number = 66;
Variables derive their power from universally available datathat 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.
myButton_btn.onRelease = function() {
gotoAndStop (myFavoriteNumber); // Moves the timeline to frame 66
cat_mc._xscale = myFavoriteNumber; // Scale cat movie clip instance 66% vertically
};

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 stateon 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.
var playMusic:Boolean = true;
NOTEIn 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 valuesoften for mathematical use. You can use numeric values for people's ages, for scores in a game, and to track the number of times someone has clicked a buttonto name just a few uses. Here's how you would create a variable and assign a number value to it:
Instead of assigning direct values (or literals)for example, 32, "dog," or something elseto variables, you can use an expression to set the value. An expression is a phraseor a collection of variables, numbers, text, and operatorsthat evaluates to a string, number, or Boolean value. For example:
var radius:Number = 32;
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 bottlesOfBeerOnTheWall:Number = 99;
var oneFellDown:Number = 1;
var bottlesLeft:Number = bottlesOfBeerOnTheWall - oneFellDown;
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.
var someone:String = "Jobe Makar";
var coffeeBoy:String = someone;
NOTEA 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:
The next script creates a variable named myVariable and places it on the root (main) timeline:
var myVariable:Number = 100;
Notice 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.Note that using a target path to create a variable restricts you from using the recommended var syntax to declare a variable. Take a look at this:
_root.myVariable = 100;
The script is not valid. Try typing it into the Actions panel and selecting the Check Syntax button. You receive an error. You can properly declare a variable on a timeline only from the timeline itself (a script attached to a frame or button in the timeline). This does not mean that you can't change the value of a variable properly from another timeline, but it's recommended that you first declare the variable in that timeline. For example, let's say you have myClip1_mc and myClip2_mc movie clip instances. In myClip1_mc you have the following code:
var _root.myVariable:Number = 100;
In myClip2_mc, you can then change the value of the variable that is living in myClip1_mc with the following code:
var myVariable:Number = 100;
By coding in this manner, you can still make use of the strict typing of variables in Flash while allowing variables to be modified from another timeline.You can also create variables directly on objects. However, you cannot use the var syntax when doing so. For example:
_parent.myClip1_mc.myVariable = 200;
It's perfectly acceptable codebut there is no way to use the var syntax for creating the name variable on the object without defining your own class of objects. (In Lesson 7, "Creating Custom Classes," you learn to create variables on an object while using the var syntax, but the concept covers new topics, so we won't discuss it here.)Because dynamic text fields can be used to display the value contained in a particular piece of data, in this exercise you set and use variables that ultimately control what's displayed on the screen for a news site project.
var person:Object = new Object();
person.name = "Jobe";
Open newsFlash1.fla in the Lesson06/Assets folder.This file currently contains no actions, but the frame and movie clip structure have already been created.The main timeline has two frame labels, Initialize and Sit. The first, Initialize, is where you create the data for this project. The other label, Sit, will contain actions used to pull out the data to be displayed in various places on screen.Move to the Sit frame label and you'll see that graphics have already been created and inserted. The screen doesn't yet contain any text fields to display data.

To structure the data for this application, you'll use objectseach of which will be named for a different day of the week and will store all the news for that day accordingly. Note that we only discuss the ActionScript for Monday and Tuesday because the ActionScript for the rest of the week works in the same way.Entering the preceding script on Frame 1 of the timeline creates the objects monday and tuesday. You add a date variable (object property) on each object to store each day's date.Save your work as newsFlash2.fla.You've created the objects, properties, and variables needed so far. You'll create more variables in a later exercise.
// ----Monday------ --
var monday:Object = new Object();
monday.date = "Monday, August 25 2003";
// ----Tuesday------ --
var tuesday:Object = new Object();
tuesday.date = "Tuesday, August 26 2003";