AS: Main, OMG OMG OMG!
So, why am I writing this? Well, for starters, there have been many complaints about the existing AS: Variables, no offense to you -Toast-.
Also, variables is an elementary part of actionscripting.
So, what is a variable?
Basically, it's a word that contains information. It can be a string, a number, or any other kind of data.
Each variable can hold one value only, but the value can be changed during runtime, allowing Flash Player to save data such as score, name, speed of an object etc. Flash Player remembers the value the variable holds, and anywhere you type the name of the variable, it recognizes the value and uses it.
Sounds good. So, how do I use these variables?
Well, first of you must declare the variable, in other words create or initialize it. This is because the variable's value might change during run-time. For example, if you have to add 10 to a variable named myScore, and myScore doesn't already have a value, Flash Player doesn't know what the new value should be.
Declaring by scoping
Whaddaya mean, scoping?
A variable's scope is a reference to where the variable "lives", in other words where it's value can be retrieved or changed. Three types of scopes exists:
and Timeline variables.
Each one of these is declared in their own way.
Local variables "lives" in one function only, the one they're declared in. When that function ends, the variable expires, it can no longer be retrieved or changed. You declare a local variable like this (Also see Strict data typing further down):
var variableName = value;
var myName = "John";
Two advantages with local variables: They make the function independent from other code in the Flash file, and there can be many local variables with the same name, assuming they are declared in different functions.
Learn more about functions in AS: Functions by Inglor.
Global variables are variables that are accessible from everywhere in your Flash file. They can be changed and read from every timeline and scope, as long as you don't have a local or timeline variable with the same name. In that case, the global variable can only be accessed from outside the other variable's scope. When you declare a global variable, you can't use var, which means you can't apply Strict data typing to a global variable. Instead, you use the word _global when declaring. Like this:
_global.variableName = value;
_global.myPassword = "3ehjTEx56";
A global variable can be declared anywhere, just remember, and this goes for all scopes, that the variable must be declared before you can access it.
Timeline variables "lives" on one timeline, the one they are declared on. I would point you to a hierarchy tutorial, but I don't know of any good one.
The variable can be accessed from anywhere on that timeline, BUT!, it can also be accessed from another timeline, using dot syntax (.). A timeline variable is declared the same way as a local variable, with the difference that it's not declared in a function (Strict data typing is applicable to timeline variables too, see further down):
var variableName = value;
var myDate = new Date();
When you have declared a timeline variable, it can be accessed from another movieclip like this:
For example, this adds the value of mySpeed on the main timeline (_root) to the property _x:
_x += _root.mySpeed;
It's also worth to note that a movieclip is also a kind of variable (Thanks Inglor for that one =P). In fact, everything that returns something else than itself can be considered, since it holds a value.
Strict data typing
When you define a local or timeline variable, you can assign them a specific data type using Strict data typing. This helps a great deal when you debug your code. Instead of having to look through a whole Flash file, a message tells you the exact spot of the error.
The pattern for defining a variable using Strict data typing is:
var variableName:variableType = value;
var tells Flash Player that a new variable is being declared.
variableName is what you want your variable to be called. This is the name you will use to access the variable's value, and the name you will use to change the value that the variable contains.
variableType tells Flash Player what kind of data the variable is supposed to hold. Use this to avoid giving a variable a value of the wrong class. Let me give an example:
var myScore:Number = 5;
This gives the variable myScore the value 5. But suppose that you later in the Flash file write this:
myScore = true;
When you then test your movie, Flash will give you an error saying "Type mismatch" and the place where you typed wrong, which helps an awful lot when you look for errors in your code.
value is, you guessed it, the value that you want to give your variable.
Naming a variable
When naming a variable, remember to always give unique names, which doesn't interfere with any of Flash's Keywords (built-in properties, methods etc.). A good way to avoid interfering with those is to use variable names like:
Also, remember that variable names is case sensitive! myScore is NOT the same as Myscore.
Deleting a variable
This is really simple. Sometimes you will not need a variable anymore, and want to free up the memory it uses. Then, simply type:
Simple as that.
Phew! I guess that's it.The hardest part of writing this was to structure it and put it in a good order, so I'm sorry if it seems a little messy. I guess I should've made a disposition, but... Oh well. Any questions, ideas or corrections? Post them here!
PS. I'm Swedish, so my use of English may be bad...