Working with Variables


If you're creating an animation that changes the color of the sky at certain times of the day, you need some basic information: What is the current time of day, and what color is the sky at different times? You can create a container in your Flash projects to hold that information. In programming, these containers are called variables.

Before you put something into a container, you need the container itself. In Flash, this process is called declaring a variable. When you first declare a variable, you typically use the keyword var and then follow it with a name for the variable. To say "I have a variable named myVariable" in ActionScript, you would enter the following code:

var myVariable;


Now you can give the named container something to hold: the value of your variable. In the following line, write the name of the newly created variable. The equal sign, also called the assignment operator, tells the compiler that you're about to tell it the value of the variable. Then you write the value. Afterward, use a semicolon to indicate that this instruction is done. Just as a period ends a sentence, a semicolon ends a statement in ActionScript. This is the syntax for assigning a value to a variable.

variableName = variable_value;


Note

You'll notice that the variable name is written with the first letter of the first word in lowercase and the second letter in uppercase. This is the most common convention for naming variableslowercaseUppercase. It's a habit I would suggest you acquire, so that others reading your code can quickly recognize variables.


Using the trace() Statement

Imagine there's a container on a shelf in your fridge with take-out from a restaurant. You might want to say, "Hey container! Whatcha got?" I don't know about the containers in your fridge, but mine generally don't answer questions. I have to open them up and look.

The engineers at Macromedia have given us a simple but wonderful tool for looking at the contents of variables while developing and testing ActionScript projects. You can ask your variables what they hold if you use the built-in trace() function. Here's an example in ActionScript where the variable timeOfDay is assigned the value of "dawn". The value of timeOfDay is then traced to the Output window:

var timeOfDay = "dawn"; trace(timeOfDay); //returns dawn


TRace is a great tool for developing in ActionScript because you can test whether certain parts of your code are performing as you expect. In other words, it can help you track down bugs and avoid creating them altogether. The trace function tries to find, in memory, the argument it's given, and then the results are traced to the Output window. These results are not visible to the end user when the published .swf file is played. In this case, the argument is the variable timeOfDay. When you test your movie (Control, Test Movie) in a Flash document with the previous code, dawn comes up in the Output window.

Tip

You can exclude trace commands from the compiled .swf file if you change the preferences in the Publish settings of your project. Go to File, Publishing Settings, and select the Flash tab (see Figure 11.3). Click on the check box next to Omit Trace Actions in the options.

Figure 11.3. Publish Settings Flash options, with Omit Trace Actions selected.



Datatypes

When you begin working with variables, Flash has a powerful way to help you prevent bugs in your projects. To help make sure that the variables you create are holding the correct type of data, you can add some information to your variables called strict typing. When a variable uses strict typing, it means it is assigned a datatype so that the debugger then gives you a specific error message if you try to assign a value of another datatype. For example, if you give the variable timeOfDay a datatype of String, you get an error if you assign it the number value of 78.5 (see Figure 11.4).

var timeOfDay:String; timeOfDay = 78.5; // gives an error message during testing


Figure 11.4. Error message in Output panel.


Datatypes are basically ways to store information with different formats. Flash has several basic datatypes, including string, number, Boolean (true/false), array, object, and date. Different datatypes can have different operations performed on them. For example, a number can be multiplied, but a string of characters cannot.

  • String A string datatype can hold a string of characters that is read as text. The characters can be letters, numerals, and other characters. String values are declared between double quotation marks.

    var greeting:String = "Hi There!";

  • Number A number datatype consists of numerical digits.

    var myAverage:Number = 54;

  • Boolean A Boolean datatype has two possible values: true or false.

    var myTest:Boolean = true;

  • Array An array datatype is a list of data values. Elements in an array are indexed by numbers, starting at zero.

    var fruitInBasket:Array = ["apples", "oranges", "bananas"];

  • Object An object is a complex datatype and can hold properties and methods. Properties are variables that are attached to an object. Both an object and its properties have names. Properties can also be other datatypes, such as arrays and even other objects.

In the following code, a new object is created and two properties of that object are declared and assigned values.

var myObject:Object = new Object(); myObject.prop1:String = "happy"; myObject.prop2:Number = 42;




Special Edition Using Macromedia Studio 8
Special Edition Using Macromedia Studio 8
ISBN: 0789733854
EAN: 2147483647
Year: 2003
Pages: 337

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net