Creating Variables

You have created many variables in Lessons 1 5. 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.


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.


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.


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 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.


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 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. 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.


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; 

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:

 var _root.myVariable:Number = 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 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:

 _parent.myClip1_mc.myVariable = 200; 

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:

 var person:Object = new Object(); = "Jobe"; 

It's perfectly acceptable code but 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.

  1. 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.


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

     // ----Monday------  -- var monday:Object = new Object(); = "Monday, August 25 2003"; // ----Tuesday------  -- var tuesday:Object = new Object(); = "Tuesday, August 26 2003"; 

    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 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.

  3. 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.

Macromedia Flash MX 2004 ActionScript(c) Training from the Source
Macromedia Flash MX 2004 ActionScript: Training from the Source
ISBN: 0321213432
EAN: 2147483647
Year: 2005
Pages: 182

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: