Actionscritp 3.0 - Tutorial 1
The variables
What are the variables?
The easiest way to think about it is as a box.
A box? Yes, just a simple box.
This box can be given a name and can contain things. For Actionscript, it will have a name and contains values or objects.
A box can contain different type of values so we will to start by telling our variable what type of values it can contain.
A variable can be local or global:
- local: if the variable is defined into a function or into a loop, so once the function code is carried out, the variable will be automatically deleted from the Flash?s memory. I will explain it to you later on in more details.
- global: this variable is always kept in memory and so is always accessible
This is the way to declare a variable:
var declare the variable to Flash
miles is the name of the variable
:Number; is the type of value that the variable can contain. A numerical value in this case.
Following the idea of the box. I tell Flash to create a box with a label name (miles) and also specify to Flash that the box can only contain values of numerical type such as 0, 3, 124, 0.4, 654.332?
Now, I will assign a value to my variable the following way:
PS: I also could have assign the value at the same time as the declaration the following way:
Code:
var miles:Number=10;
Little parenthesis: :Number is define as the Data Type of the variable.
With Actionscript 1.0, we were used to simply write var miles=10; and in the OOP there is nothing worst then doing so.
As an advice, I recommend you to always specify the Data Type. If you wanted next to start writing code on an AS file, without the Data Type, Flash would return an error. So better get used to it from now on.
Next, I write:
I tell Flash to show me what is in the box, so flash will open the box and will show me the value contained into it.
I obtain the following output:
10
I said earlier on that the variable Miles can contain only numerical values as it has been data typed as Number (:Number).
Let us see if it is correct and try to insert a value of String type into the variable miles:
if I publish the SWF, Flash would return the following error:
1067: Implicit coercion of a value of type String to an unrelated type Number.
I tells me that the type of value is wrong and that I am trying to assign a String value to a variable data typed Number:
Instead, if after writing var miles:Number=10; I would add:
with a trace(miles), I would obtain the following output:
10
15
We can understand now that we can change the value of the variable as wanted keeping in mind to always use the same data type declared
With the following example, I add a value to the existing value of miles:
this syntax tells Flash:
open the box named miles, take its value, add 10 and close again the box.
Therefore, it adds a value of 10 to the already existing value.
In fact with trace (miles); I get the following output:
10
15
25
It is the same as saying miles= miles+10;
The spontaneous question is which types of values can I assign to a variable?
Answer: every type of value of the built-in classes that exist in Flash, therefore Number, String, MovieClip, TextField, etc etc
A complete list of the existing classes in Flash can be found to this link:
Migration
To help you better to understand, here are some few examples.
I create a variable of type String and I assign to it a string value:
Code:
var my_name:String="filippo";
and with trace(my_name) I obtain the following output:
filippo
it is important to write the string (as it is a string) in between the double quote ?filippo? otherwise if we would write:
Code:
var my_name:String=filippo;
Flash would return the following error:
1120: Access of undefined property filippo.
rightly telling us that it cannot find any variable named filippo.
In fact, if we write =filippo, Flash is looking for a variable with such name to assign its value to the variable ?my_name?.
Another example, I create a variable of type MovieClip:
Code:
var clip:MovieClip=new MovieClip();
in this case, we have a new syntax not seen before: the operator new.
This operator allows Flash to understand that it has to create a new MovieClip as the variable clip will contain a MovieClip.
So, using var clip:MovieClip=new MovieClip(); we are telling Flash to create a new empty MovieClip.
Going further on, we should then create an instance of the MovieClip class but it is not yet the right time to get into this and at this point would only confuse the basic idea.
Another example, I create a variable a type textField:
Code:
var field:TextField=new TextFiled();
once again, we find the operator new.
Stay tuned to learn more about it!
Source files: