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. The following is the syntax for creating a variable:

 myFullName = "Jobe Makar"; 

The ActionScript to the left of the equals sign represents the variable name; the portion to the right represents its value.


You can name a variable anything you choose, as long as you follow these simple rules: It must begin with a character, and it cannot contain spaces, special characters (@, #, $, %, and so on), or punctuation marks.


Name 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've created a variable and assigned it a value, you can use that value in any script simply by referencing its name. Take a look at the following:

 favoriteNumber = 66; 

This creates a variable named favoriteNumber and assigns it a value of 66. To use that value in a script, you would use syntax similar to the following:

 on (release) {    gotoAndPlay (favoriteNumber); // Moves the timeline to frame 66      cat._xscale = favoriteNumber; // Scale cat movie clip instance  66%  vertically  } 

As you can see, 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, since some of the actions are based on the current value of that variable.


There are three main types of variable values:

  • String

  • Boolean

  • Number

A string is a text value. For instance, myFullName = "Jobe Makar" has the string value "Jobe Makar." Strings are most often used to store text, such as latestNews = "We just got a new dog!" String values are defined using quotation marks (""). Thus, the syntax myFavoriteNumber = "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:

 playMusic = true; 


In ActionScript, true has a numerical value of 1 and false has a numerical value of 0. Choosing to use true/false or 0/1 when assigning Boolean values is a matter of preference.

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 (musicPlaying = true or musicPlaying = false ). You could attach a script to the button so that when pressed, it would check to see if the music was on or off (true or false ). If the music were currently on (musicPlaying = 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 (musicPlaying = false ), the music would need to be turned on and the value of the variable set to true . Since the value of musicPlaying is being 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.


In Lesson 9, Conditional Logic, we'll cover Boolean values (and their uses) in more depth.

A number value is just what it sounds like, a number. Numbers are used to store numeric values often 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 button to name just a few uses. The following demonstrates how you would create a variable and assign a number value to it:

 radius = 32; 

Instead of assigning direct values (or literals) for example, 32, "dog," or something else to variables, you can use an expression to set a variable's value. An expression is a phrase or a collection a variables, numbers, text, and operators that evaluates to a string, number, or Boolean value. For example:

 bottlesOfBeerOnTheWall = 99;  oneFellDown = 1;  bottlesLeft = 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 then performs a subtraction. The end result is that the variable bottlesLeft is assigned a value of 98. It's important to note that depending on how the expression is structured, 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 the following example:

 someone = "Jobe Makar";  coffeeBoy = someone; 

In this example, a variable called someone is created in the first line of ActionScript. 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 at a later time, coffeeBoy will not reflect the change.


A variable can also gain its value as the result of a function. For more on this, 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 which timeline it was placed when it was created. The following ActionScript creates a variable named myVariable in the current movie:

 myVariable = 100; 

The following creates a variable named myVariable and places it on the root, or main, timeline:

 _root.myVariable = 100; 

Notice the use of a target path in this variable's creation. By using target paths in this manner, you can create or access data on a specific timeline.



If a project is not working as it should, check for errors in target paths to pieces of data. A single line of script that tries to access a piece of data on an incorrect timeline can cause major problems in your applications.

Since dynamic text fields can be used to display the value contained in a particular piece of data, in this exercise you'll set and use variables that will ultimately control what's displayed on the screen for the news site project.

  1. Open newsFlash1.fla in the Lesson07/Assets folder.

    This file currently contains no actions, but the frame and movie clip structure have already been created.

    There are three frame labels on the main timeline: initialize, refresh, and sit. The first label, initialize, is where you'll create the data for this site. The next label, refresh, will contain actions used to pull out the data to be displayed in various places on screen. And the final frame, sit, is just a place where the playhead can rest until we need the screen to change its display again.

    Move to the refresh 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.


  2. Select Frame 1 of the Actions layer on the main timeline. Open the Actions panel and enter the following ActionScript:

     // ----Monday-------- monday = new Object(); = "Monday, August 20 2001";  // ----Tuesday-------- tuesday = new Object(); = "Tuesday, August 21 2001"; 

    To structure the data for this application, you'll use objects each of which will be named for a different day of the week and will store all of 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.

    By entering the above ActionScript on Frame 1 of the timeline, you create two objects, monday and tuesday . You also add a date variable (or object property) on each object to store each day's date.

  3. Save your work as newsFlash2.fla.

    You've created the variables needed thus far. You'll create more variables in a later exercise.

Macromedia Flash MX ActionScripting Advanced. Training from the Source
Macromedia Flash MX ActionScripting: Advanced Training from the Source
ISBN: 0201770229
EAN: 2147483647
Year: 2002
Pages: 161 © 2008-2017.
If you may any questions please contact us: