Variables
In a programming language you need several things:
1. Data
2. Ways of evaluating the data
3. Ways of manipulating the data
These exist in a programming language as:
1. Variables
2. Operators
3. Functions/Expressions
A variable is simply a container, a bucket if you will. However this bucket can only contain one piece of information at any one time, no matter how small. Derek Franklin, Author of Flash 4! related variables to real life. Consider the information in your life. These could be variables too:
Name = "Eddie"
Age = 89
Income = "not enough"
Friends = 0
You will notice here that each line can be broken up into several parts - the variable name, the operator and the value. The operator will be dealt with in a later tutorial but for now we will be using the equals sign (=).
The Variable Name
The variable name can be any letter, number or underscore and are not case sensitive
but must begin with a character. These names should also hold meaning too and
while their values may change the name doesn't. There is not much point in calling
a variable that holds people's names, 'x' or 'cow'. There are also some other
important things about variables that I should mention.
The value assigned to a variable can be one of four types, a number, a boolean, a string or nothing. (you can assign a variable another variable or expression but these will always evaluate to one of the base types).
Number - Any number. eg 12, -1, 34534.
Set Variable: "myvariable" = 12
Boolean - True or False.
Set Variable: "myvariable" = True
String - Any number or letter presented in Quote marks. eg "hello"
or "12".
Set Variable: "myvariable" = "12 Chifley St"
Nothing - Used by actionscripters to define the lack of a string. eg ""
Set Variable: "myvariable" = ""
While most of these types are pretty self-explanitory you should always keep in mind that they are different to each other and require different treatment in actionscripting. A number is simply any number. A string is a programmers way of describing text. Note that in the examples above I've used the number 12 in the string example. Numbers can be string when enclosed by the quotation marks.
Boolean is the type used for action that contain conditions like the If statement or the Loop statement. When using the boolean type something can be either True or False. Some programmers choose not to use these words but instead use the number 0 to indicate False and any non-zero number to denote True (usually 1).
Nothing is simply the lack of any text (string). In flash many use it to check when the last variable has been processed in a loop function or to see if the last variable in a text file has been loaded. (note: the ... means other codes has been left out of the example)
Loop While (eof < 1)
...
If ("button"&n = "")
Set Variable: "eof" = 1
End If
...
End Loop
One of the single most annoying causes of errors in actionscript are the Quote Marks " ". Why? Because this - "myvariable" is completely different to this - myvariable. The first is a literal (a string) and the second is an expression.
A literal assignment is the simplest method of assignment and is literally (excuse the pun) some information placed into a container. For example,
Set Variable: "myvariable" = "counter"
This is is NOT assigning the value of the variable "counter" to the variable "myvariable". It is assigning the text (or string) "counter". But the power of actionscripting is its ability to assign expressions to variables. An expression can be one or more variables, numbers or strings (but more on this in the next tutorial). Now let's take a look at these examples to see variables and expressions in action: (the variables are green and the expressions are red - remember expression can contain variables too!)
Set Variable: "myvariable" = othervariable
Set Variable: "Counter" = Counter + 1
Set Variable: "subtotal_one" = 20
Set Variable: "subtotal_two" = 45
Set Variable: "total" = subtotal_one + subtotal_two
Set Variable: "city" = "Sydney"
Set Variable: "country" = "Australia"
Set Variable: "home" = city & country