Overview
Lines of code need to be terminated with a semi-colon. All variables need to be defined using the “var” keyword. All variables need to have a class type specified. Blocks of code are defined by {} brackets.
Temp variables
Temp variables are created with the use of the keyword “var”. In actionscript 3 variables are instances of classes which means that when creating a variable you need to set the class.
|
var myTempVariable:String = “Hello World”; |
A few useful classes are as follows: String, Number, Boolean, Array, ArrayCollection, File, FileStream, etc. Temp variables are only usable within the function that they were created.
Packages & Classes
Classes are contained within packages. Your Packages are folders within the root of your application’s folder. For example if we wanted a class called MyClass within the package myPackage then you would create a folder called myPackage and then create MyClass.as within that. Note that packages are written in camel case and classes have the first letter capitalised in each word.
The contents of MyClass.as should look like this:
package myPackage
{
public class MyClass
{
public function MyClass()
{
}
}
}
|
To create an instance of your new class, you first need to import it with the following code:
|
import myPackage.MyClass; |
Then to create an instance use the “new” keyword:
|
var tVar:MyClass = new MyClass(); |
When this line of code runs, the function “MyClass” is executed, so any initiation code needs to be placed in there.
Class Variables
Classes can have several types of variables: public, private and internal. Public variables are accessible from anywhere, private variables are accessible only from within the class and internal variables are only accessible from within the classes package.
Functions
Functions can be either in classes or the application its self. Functions can either return nothing or return a variable. The code for a function is as follows:
package myPackage
{
public class MyClass
{
public function MyClass()
{
}
public function myTestFunction(inVar:String):Number {
return Number(inVar);
}
}
} |
The function “myTestFunction” takes a string and returns it as a number. If you don’t want to return a variable then give the function a type of :Void. Functions like variables can be public, private or internal. If your function is going to be run after an event then the function needs to be public.
Maths
Here are some examples of how to do calculations.
To add 5 to a variable you would write the following:
To subtract 5 from a variable you would write the following:
To multiply a variable by 5 you would write the following:
To divide a variable by 5 you would write the following:
There are some useful maths functions, a few are listed below:
myVar = Math.round(inNumber);
myVar = Math.random();
myVar = Math.PI;
myVar = Math.sin(inRads);
myVar = Math.cos(inRads);
|
Array & ArrayCollection
Array is quicker and more memory efficient than ArrayCollection. Use Array to store data that needs to be searched or moved around, use ArrayCollection to bind to display objects like ComboBox because bindings will only update with array collections.
To create a new Array write the following code:
|
var myArr:Array = new Array(); |
Now to add some values use the “push” or “unshift” functions:
myArr.push(“String1”);
myArr.unshift(“String2”); |
“push” adds the variable to the end of the Array, “unshift” adds the variable to the beginning of the Array so myArr now represents [“String2”,”String1”]. You can access variables stored in an Array by using square brackets, Arrays in Actionscript are zero based:
To remove variables use the “pop” or “shift” functions:
myVar = myArr.pop();
myVar = myArr.shift(); |
“pop” removes and returns the last variable in the Array, “shift” removes and returns the first variable in the Array. To delete items in an Array us the “splice” function:
The first value is the start index and the second is the delete count, so this would delete the second variable in the Array. ArrayCollection is similar to Array but has different functions to control them. To Add an item to the end of an ArrayCollection use the “addItem” function:
|
myArrC.addItem(“Hello World”); |
To Add an item at a specific index use the “addItemAt” function:
|
myArrC.addItemAt(“Hello World”,5); |
This would add “Hello World” to index 5. To get a variable at an index use the “getItemAt” function:
|
myVar = myArrC.getItemAt(5); |
To delete a variable from an ArrayCollection use the “removeItemAt” function:
Loops
There are two types of loops, “while” loops run until a condition returns true, “for” loops run a set number of times.
“while” loops need to be thought out carefully as it’s easy to create a infinite loop and lock up your application. An example of a use for a “while” loop would be to create a temp folder that doesn’t already exist:
var tFile:File = new File();
tFile.nativePath = “C:/temp/tmp”+String(Math.round(Math.random()*999));
while(tFile.exists){
tFile.nativePath = “C:/temp/tmp”+String(Math.round(Math.random()*999));
} |
In this example the loop would run until it found a folder that doesn’t exist.
“for” loops are for a set number of runs, a good example would be to loop through an Array and perform a calculation on the variables contained in the Array:
var i:Number;
for (i=0;i<myArr.length;i++){
myArr[i]++;
} |
In this example the loop adds 1 to every variable in the Array.
Conditional Jumps
There are two ways to control the flow of execution “switch” and “if” statements. “if” statements use conditional statements to control if code executes:
var i:Number = 5;
if (i==5){
i++;
}elseif(i>=6){
i--;
}else{
i = 0;
} |
Notice the use of == in the conditional statement, don’t use = as this will set the variable on the left to the value on the right and will always return true. “switch” statements check that a value matches:
var i:Number = 8;
switch (i){
case 5:
i++;
break;
case 8:
i--;
break;
} |
|