Flex Tutorial: A Quick Course On Actionscript
In the previous chapter we discussed the steps involved in creating flex application. This lesson will discuss actionscript syntax. Actionscript was created by Adobe for their Flash IDE to control 2D animations in flash. The actionscript syntax is very similar to that of Javascript. So if you know Javascript 80% of actionscript you know most of actionscript’s syntax.
In this article I will go into the additional features that are supported by Actionscript.
Variable Declarations
Syntax:
var variableName;
Sample:
var sample_var = 1;
Variable name rules: Name should not contain spaces, cannot be reserved actionscript keywords like if, for, do, while.
Strings
Strings are assigned to variables using a single quote or double quote.
var name = ‘raj’; var anothername=”raj”;
Booleans
Booleans are assigned to either true or false. The words are lowercase and are reserved keywords.
var isAMonkey = true;
Strict Data Typing
Although variables are loosely typed it is possible to specify a type for your variables in Actionscript. This is also called variable type annotation. The following statement declares a variable of type Number. You can specify annotations for your own data type declarations also.
var mileage:Number=20; var Ferrari:Car;
Functions
Functions are defined with the same syntax as Javascript. But actionscript supports specifying return type through type annotations similar to variable type annotations. Shown below are some sample declarations
function findInterest(principal,interest,time):number
{
…
}
Anonymous functions
An anonymous function is the same as normal functions but it is defined similar to declaring a variable.
var testFunction = function() {
doSomethin();
}
testFunction();
The difference is that anonymous functions should be fully defined before being executed but normal functions will get mapped to their definitions even if the definition falls after the part where the function is called.
Flow control and looping
Actionscript supports all the looping constructs that Javascript does:
While loops
while (expression)
{
//do something
}
For Loops
var y:Number=1;
for (var x:Number = 1; x <=5; x++)
{
y=y*x;
}
Classes And Objects
Actionscript supports classes in a way similar to Javascript. Instead of relying on the function keyword to define functions, actionscript uses the following syntax:
Class Classname
{
private var varName;
public function funcName()
{
}
}
Here public and private are access specifiers. The first definition within the class is a class attribute. The second is a class method. Instantiating an object:
var newObj = new ClassName();
If you are familiar with Java you will find actionscript’s syntax of defining classes very similar.
Packages
Packages are used to group related classes together.
package packageName
{
//class definitions
}
Static variables
Static variables are attributes that are defined as part of the class. These variables are not unique to each variable like instance variables. They are accessed by referencing the classname.
class ActionscriptBook
{
public static pages:Number = 322;
}
var pages = ActionscriptBook.pages;
Getters and Setters
Actionscript supports creating getter and setter functions that control access to class variables.
class Box {
private var side:Number;
public function get theSide():Number
{
return side;
}
public function set theSide(theNum:Number):Number
{
side=theNum;
}
}
In this chapter we have learnt the basic actionscript syntax. In the next chapter, we will learn how to use the various components and how to place them within containers.
Steven November 17th, 2009 at 7:11 pm
Hi, Raj. Congrats on a useful tutorial. Good luck!