• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint


All sophisticated programming languages use variables to store information. You may store information for a number of reasons. A variable has two parts: its name and a value.

Storing Information

You will need to store information to write complex computer programs. Sometimes you will only store the information for a short period of time. For instance, you may want your program to repeat a set of commands 10 times. In that case, you will need to count the number of times the commands have been repeated and continue on after this counter reaches 10.

In other cases, you will want to store information for longer periods of time. For instance, you can ask the user to enter her name and then you can store this information in a variable. Later in the Flash movie, you can display this name.

Variable Names

The name of a variable is usually a word or group of words. Sometimes it is a single letter. In general, you should make the name of the variable as descriptive as possible.

For instance, if you want to store the name of the user in a variable, userName is a good choice for the variable name. A bad choice would be n, which is too short, or name, which could be confused with the name of something else in the Flash movie.

In ActionScript, a convention has arisen that many programmers follow when inventing variable names. The variables usually start with a lowercase letter but then use an uppercase letter when a new word begins in the name. The variable name userName is a good example. A longer example would be currentUserFirstName.

Spaces cannot be used in variable names, and neither can special characters. But numbers can be used. So a variable name may be player2.

Variable Types

You can store many different types of information in variables. Numbers are the simplest. You can store the number 7 in a variable, for instance.

You can store two different types of numbers in variables. An integer is a number that has no decimal component. The numbers 7, 335, -5, and 0 are all good examples of integers.

The other type of number is called a floating point number, or float. This is a number that has a decimal component. Good examples of floats include 0.1, 532.23, and -3.7.

You can store characters and words in variables too. These are called strings. A string is a sequence of characters. It can be one or more characters, or even a string without characters, which is called an empty string. “Hello”, “This and that”, “a”, and “” are examples of strings.

When writing about strings, quotes are used to define that the value is a string and not another type of variable. For instance, 7 is the number 7, whereas “7” is a string with a single character, the digit 7.

In other programming languages, you would normally have to decide in advance what type of information each variable would hold and write that at the beginning of your program. For instance, you would declare that there is a variable named userName and that it holds a string. However, in ActionScript, you don't have to declare variables in advance. You just use them, and Flash creates the variables on-the-fly the first time they are used.

In addition, the variables are not restricted to holding only one type of information. The variable userName could hold a string at one point and a number at another point.

This flexibility is rarely needed, but it is one less thing for ActionScript programmers to worry about.

Another thing that ActionScript programmers don't have to worry about is garbage collection. This is when you reclaim the memory used by a variable after the variable has ceased to be useful. Most modern computer languages such as ActionScript automatically perform garbage collection, so you don't have to worry about it.

There are other types of variables besides numbers and strings. For instance, arrays can hold lists of information instead of a single piece of information. We'll look at arrays and other data types later in the book as they are needed.

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint