Variable Types: Variables are used to store a variety of different types of information, from street addresses to a person's age, so some form of distinction between variable types is required. Since the names we give our variables can be almost completely arbitrary, Flash needs some other indication as to the type of information we are storing, so that it is able to manipulate the information later on should we wish it. For instance, there is no point in trying to multiply two people's names together; such an operation is only reasonable for Numeric values. A variable's datatype relates to the information the variable stores and assists Flash in determining which actions are appropriate to invoke on this information. In this tutorial we will discuss the following basic datatypes: Strings, Numbers and Booleans.
Numbers
Numbers are an easy concept to grasp; you've been using them since you were a child. Numeric variables in Flash are variables whose values can be manipulated using mathematical expressions like multiplication. For instance, if we wished to record the year in which ActionScript.org was launched, we would generally do it as a number, in the following form:
[as]founding_year = 2000;
[/as]
If we want to determine how long this site has been operational and we have another variable which stores the current year as a numeric value, we can subtract the founding year from the current year to get a third numeric variable, as follows:
[as]founding_year = 2000;
current_year = 2003;
operational_for = current_year - founding_year;[/as]
In the above example, the operational_for variable will be created an allocated the numeric value 3. Let's move on to some other variable types and the distinction should become clearer.
Strings
Strings are one or more characters (letters, digits, spaces, etc.) tied together. In general this includes things like names, addresses, and other information which can't be manipulated in the same way as numbers. String values are signified in Flash by enclosing the text we wish to store in our variable in double or single quotes. For example, the following statements both create a variable called country which stores the word Australia.
[as]country = 'Australia';
country = "Australia";[/as]
If you were to enter either of the above lines without the use of quotes, you would be telling Flash to assign the variable called country the same value as the variable Australia.
Whenever you wish to represent a phrase, sentence or sequence of characters, you will generally use a String. Strings are also the type of data displayed in TextFields in Flash, although the TextField Object is beyond the scope of this tutorial.
If the distinction between Numeric variables and String variables is still not clear, consider the following example:
[as]my_sum = 1 + 2 + 3;[/as]
Written in this form, the above ActionScript will create a new variable called my_sum, evlauate the expression on the right hand side of the statement, and assign the new variable a value of 6. However, if we wished to actually represent the equation rather than its result, we would do so using a String value, as the String will not be interpreted, rather, Flash will store it exactly as we provide it:
[as]my_sum = "1 + 2 + 3";[/as]
Booleans
Boolean variables might be a foreign concept to you if you have never programmed before. Boolean variables store one of two values: true or false. When you get into more complicated ActionScript such as conditionals and loops you will use Boolean values often. For now just understand that they store a logical representation of either true or false which can be used in making decisions. If you wanted to create a Boolean variable which shows that you like Flash, you would do so as follows:
[as]likeFlash = true;
[/as]
There's another test for you over the page...