ActionScript is a scripting language (Developed by Macromedia,now owned by Adobe ) based on ECMAScript (Also referred as JavaScript or Jscript), used primarily for the development of websites and software using the Adobe Flash Player platform.
The latest ActionScript 3.0 introduced last June (2006) with Adobe Flex 2.0 and Flash player 9.0. This ActionScript is completely different than any of its predecessor since it uses an entirely different virtual machine. ActionScript 3.0 provides significant enhancement in performance, as well as more robust programming model that lends itself to complex Rich Internet Application development. You can say that’s the reason Flex is so kick ass RIA builder.
How ActionScript work?
The way ActionScript work is very simple like any other scripting you have known. Generally ActionScript is executed by the ActionScript Virtual Machine (AVM), which is part of Flash Player. ActionScript code is typically compiled into bytecode format by a compiler, such Flex Builder, or that is available in the Flex SDK and the Flex Data Services. The bytecode is embedded in SWF files, which are executed by the Flash Player, the run-time environment. Pretty simple and straight forward isn’t it?
Why ActionScript 3.0?
As you already know since Flex 2, all flex version use ActionScript 3.0, but what are the advantages of this? This goes beyond the scripting capabilities of previous versions of ActionScript. It is designed to facilitate the creation of highly complex applications (RIAs) with large data sets and object-oriented, reusable code bases. While ActionScript 3.0 is not required for content that runs in Adobe Flash Player 9, it assists to performance improvements that are only available with the AVM2, the new virtual machine. ActionScript 3.0 code can execute up to ten times faster than legacy ActionScript code. You must know Flash Player 9 still do support the older version of ActionScript Virtual Machine, AVM1 for backward compatibility with existing and legacy content.
Apart from the above mentioned advantages ActionScript also poses features like- ECMAScript for XML, native support for regular expressions, a method closure for better event handling, sealed classes for improved memory usage and run-time exception for handle errors robustly.
Usage
There are two methods for using ActionScript in a Flex application-
- <mx:Script> tag, which lets you insert ActionScript code as if it was typed directly within the <mx:Script> tag.
- If you want smarter way to organize your code than the best method is always storing your code in an ActionScript file. An ActionScript source file is just a text files with the .as extension, just like CSS file. You can call this in MXML using these methods-
- Using <mx:Script> tag which lets you specify a source attribute that identifies an external ActionScript file to be loaded at a specific point in the application. For example- <mx:Script source=”example.as” />
- In this method you have to use the import statement to specify the full name of the class (Case sensitive), so the ActionScript compiler knows where to find it. For example, if you want to use the FlexExam class in ActionScript, you first need to import that class using its full name, including package and class: import flash.tutor.FlexExam;
- Through code which specifically refers to the class name. It works very simple, by referring to another class name in ActionScript code; you tell the compiler to load the definition of that class. For example, given an external class called Exx, this statement causes a new instance of the Exx class to be created: var sqr:Exx = new Exx(40,20);
Step by step building a simple ActionScript file for Flex
- You should have an MXML file called “test.mxml” created either in notepad or through Adobe Flex Builder 2
(sponsored link). The content of the file is:
- < ?xml version="1.0" encoding="utf-8"?>
- <mx :Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete = "initApp()" >
- </mx><mx :Script>
- < ![CDATA[
- private var myFirst:Hello = new Hello();
- public function initApp():void
- { <br />
- // says hello at the start
- mainTxt.text = myFirst.sayHello();
- }
- ]]>
- </mx>
- Now create a new ActionScript file using flex builder (File > New > ActionScript File) or in notepad (remember to use all files and give extension of you file .as while using notepad). For example the ActionScript file name is “Hello.as”. The content of the file is:
- package
- {
- public class Hello
- {
- public function sayHello():String
- {
- var greeting:String;
- greeting = "Say To Hello Flex!";
- return greeting;
- }
- }
- }
- Now you got two files-one mxml that calling the ActionScript and second the ActionScript file. Just save both of them, if you are using Flex builder, save the project file as well and run.
This is just a basic demonstration of ActionScript usage in a Flex environment; you can perform a lot more complex task using ActionScript on the fly.
Data Types and Variable
Object is the centre of ActionScript, every variable you declare, every function you write, and every class instance you create is an object. But note that ActionScript objects are a lot different than the Objects in OOP programming like Java or C++. Since that is a lot more advance topic we will put it out for later series of flex tutorials.
In ActionScript 3.0, every object is defined by a class. A class can be thought of as a template or a blueprint for a type of object. Class definitions can include variables and constants, which hold data values, and methods, which are functions that encapsulate behaviour bound to the class. You can define your own classes using the class keyword. You can declare class properties in three ways: constants can be defined with the const keyword, variables are defined with the var keyword, and getter and setter properties are defined by using the get and set attributes in a method declaration. You can declare methods with the function keyword. You create an instance of a class by using the new operator.
Here is another interesting thing you should be aware of that is -Packages and namespaces, which are related concepts. Packages allow you to bundle class definitions together in a way that facilitates code sharing and minimizes naming conflicts. Namespaces allow you to control the visibility of identifiers, such as property and method names, and can be applied to code whether it resides inside or outside a package. Packages let you organize your class files, and namespaces let you manage the visibility of individual properties and methods. The example above is the simple demonstration of what package can be.
In ActionScript 3.0, we use the var statement for declaring variables. To associate a variable with a data type, you must do so when you declare the variable. You designate a variable’s type by appending the variable name with a colon (:), followed by the variable’s type. You may as well assign a value to a variable using the assignment operator (=).If you have more than one variable to declare, you can declare them all on one line of code by using the comma operator (,) to separate the variables, just like a C++. The primitive data types include Boolean, int, Null, Number, String, unit, and void. For reference you must know that the ActionScript core classes also define the following complex data types: Object, Array, Date, Error, Function, RegExp, XML, and XMLList.
This was just an overview or basic article to give an idea of ActionScript in relation of Flex application. Under any circumstances do not consider this as ActionScript tutorial, since ActionScript is a lot more vast and deep to cover in just few posts. We will continue to explore more about ActionScript and its syntax as we go further with our Flex tutorial.
Leave a Comment
If you would like to make a comment, please fill out the form below.-(See Privacy Policy)









[...] (more…) [...]