Thursday, June 19, 2008

ActionScript

About ActionScript

ActionScript is the programming language for the Adobe® Flash® Player and Adobe® AIR™run-time environments. It enables interactivity, data handling, and much more in Flash, Flex, and AIR content and applications.

ActionScript is executed by the ActionScript Virtual Machine (AVM), which is part of Flash Player and AIR. ActionScript code is typically compiled into bytecode format (a sort of programming language that's written and understood by computers) by a compiler, such as the one built into Adobe® Flash® CS3 Professional or Adobe® Flex® Builder™, or that is available in the Adobe® Flex® SDK. The bytecode is embedded in SWF files, which are executed by Flash Player and AIR.

ActionScript 3.0 offers a robust programming model that will be familiar to developers with a basic knowledge of object-oriented programming. Some of the key features of ActionScript 3.0 include the following:

  • A new ActionScript Virtual Machine, called AVM2, that uses a new bytecode instruction set and provides significant performance improvements
  • A more modern compiler code base that adheres much more closely to the ECMAScript (ECMA 262) standard and that performs deeper optimizations than previous versions of the compiler
  • An expanded and improved application programming interface (API), with low-level control of objects and a true object-oriented model
  • A core language based on the upcoming ECMAScript (ECMA-262) edition 4 draft language specification
  • An XML API based on the ECMAScript for XML (E4X) specification (ECMA-357 edition 2). E4X is a language extension to ECMAScript that adds XML as a native data type of the language.
  • An event model based on the Document Object Model (DOM) Level 3 Events Specification

Advantages of ActionScript 3.0

ActionScript 3.0 goes beyond the scripting capabilities of previous versions of ActionScript. It is designed to facilitate the creation of highly complex applications 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 or AIR, it opens the door 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.

The older version of ActionScript Virtual Machine, AVM1, executes ActionScript 1.0 and ActionScript 2.0 code. AVM1 is supported by Flash Player 9 for backward compatibility with existing and legacy content.

What's new in ActionScript 3.0

Although ActionScript 3.0 contains many classes and features that will be familiar to ActionScript programmers, ActionScript 3.0 is architecturally and conceptually different from previous versions of ActionScript. The enhancements in ActionScript 3.0 include new features of the core language and an improved Flash Player API that provides increased control of low-level objects.

Note: Adobe® AIR™ applications can also use the Flash Player APIs.

Core language features

The core language defines the basic building blocks of the programming language, such as statements, expressions, conditions, loops, and types. ActionScript 3.0 contains many new features that speed up the development process.

Subtopics



Run-time exceptions

ActionScript 3.0 reports more error conditions than previous versions of ActionScript. Run-time exceptions are used for common error conditions, improving the debugging experience and enabling you to develop applications that handle errors robustly. Run-time errors can provide stack traces annotated with source file and line number information, helping you quickly pinpoint errors.

Run-time types

In ActionScript 2.0, type annotations were primarily a developer aid; at run time, all values were dynamically typed. In ActionScript 3.0, type information is preserved at run time, and used for a number of purposes. Flash Player 9 and the Adobe AIR runtime perform run-time type checking, improving the system's type safety. Type information is also used to represent variables in native machine representations, improving performance and reducing memory usage.

Sealed classes

ActionScript 3.0 introduces the concept of sealed classes. A sealed class possesses only the fixed set of properties and methods that were defined at compile time; additional properties and methods cannot be added. This enables stricter compile-time checking, resulting in more robust programs. It also improves memory usage by not requiring an internal hash table for each object instance. Dynamic classes are also possible using the dynamic keyword. All classes in ActionScript 3.0 are sealed by default, but can be declared to be dynamic with the dynamic keyword.

Method closures

ActionScript 3.0 enables a method closure to automatically remember its original object instance. This feature is useful for event handling. In ActionScript 2.0, method closures would not remember what object instance they were extracted from, leading to unexpected behavior when the method closure was invoked. The mx.utils.Delegate class was a popular workaround, but it is no longer needed.

ECMAScript for XML (E4X)

ActionScript 3.0 implements ECMAScript for XML (E4X), recently standardized as ECMA-357. E4X offers a natural, fluent set of language constructs for manipulating XML. In contrast to traditional XML-parsing APIs, XML with E4X performs like a native data type of the language. E4X streamlines the development of applications that manipulate XML by drastically reducing the amount of code needed. For more information about the ActionScript 3.0 implementation of E4X, see Working with XML.

To view ECMA's E4X specification, go to www.ecma-international.org.

Regular expressions

ActionScript 3.0 includes native support for regular expressions so that you can quickly search for and manipulate strings. ActionScript 3.0 implements support for regular expressions as they are defined in the ECMAScript (ECMA-262) edition 3 language specification.

Namespaces

Namespaces are similar to the traditional access specifiers used to control visibility of declarations (public, private, protected). They work as custom access specifiers, which can have names of your choice. Namespaces are outfitted with a Universal Resource Identifier (URI) to avoid collisions, and are also used to represent XML namespaces when you work with E4X.

New primitive types

ActionScript 2.0 has a single numeric type, Number, a double-precision, floating-point number. ActionScript 3.0 contains the int and uint types. The int type is a 32-bit signed integer that lets ActionScript code take advantage of the fast integer math capabilities of the CPU. The int type is useful for loop counters and variables where integers are used. The uint type is an unsigned, 32-bit integer type that is useful for RGB color values, byte counts, and more.


Using ActionScript in Flex applications

Flex developers can use ActionScript to implement custom behavior within their Flex applications. You first use MXML tags to declare things like the containers, controls, effects, formatters, validators, and web services that your application requires, and to lay out its user interface. Each of these components provides the standard behavior you'd expect. For example, a button automatically highlights when you roll over it, without requiring you to write any ActionScript. But a declarative language like MXML is not appropriate for coding what you want to happen when the user clicks a button. For that, you need to use a procedural language like ActionScript, which offers executable methods, various types of storage variables, and flow control such as conditionals and loops. In a general sense, MXML implements the static aspects of your application, and ActionScript implements its dynamic aspects.

ActionScript is an object-oriented procedural programming language, based on the ECMAScript (ECMA-262) edition 4 draft language specification. You can use a variety of methods to mix ActionScript and MXML, including the following:

  • Use ActionScript to define event listeners inside MXML event attributes.
  • Add script blocks using the tag.
  • Include external ActionScript files.
  • Import ActionScript classes.
  • Create ActionScript components.

ActionScript compilation

Although a simple Flex application can be written in a single MXML or ActionScript (AS) file, most applications will be broken into multiple files. For example, it is common to move the and blocks for an into separate AS and CSS files which the application then includes. It is also common for an application to import custom MXML and ActionScript components. These must be defined in other files, and MXML components may put their own blocks into yet more AS files that they include. Components may also be imported from precompiled SWC files rather than source code. Finally, SWF files containing executable code can also be embedded in an application. The end result of all these input files is a single SWF file.

The Flex compiler transforms the main MXML file and other files it includes into a single ActionScript class. As a result, you cannot define classes or use statements outside of functions in the MXML files and the included ActionScript files.

You can reference imported ActionScript classes from your MXML application files, and those classes are added to the final SWF file. When the transformation to an ActionScript file is complete, Flex links all the ActionScript components and includes those classes in the final SWF file.

About generated ActionScript

When you write an MXML file and compile it, the Flex compiler creates a class and generates ActionScript that the class uses. MXML tags and ActionScript are used by the resulting class in several ways. This information is useful for understanding what is happening in the background of the application.

An MXML application (a file that starts with the tag) defines a subclass of the Application class. Similarly, an MXML component (a file that starts with some other component's tag, such as ) defines a subclass of that component.

The name of the subclass is the name of the file. The base class is the class of the top-level tag. An MXML application actually defines the following:

class MyApp extends Application

If MyButton.mxml starts with , you are actually defining the following:

class MyButton extends Button

The variable and function declarations in an block define properties and methods of the subclass.

Setting an id property on a component instance within a class results in a public variable being autogenerated in the subclass that contains a reference to that component instance. For example, if the tag is nested deeply inside several containers, you can still refer to it as myButton.

Event attributes become the bodies of autogenerated event listener methods in the subclass. For example:



becomes

private function __myButton_click(event:MouseEvent):void {
foo = 1;
doSomething()
}

The event attributes become method bodies, so they can access the other properties and methods of the subclass.

All the ActionScript anywhere in an MXML file, whether in its block or inside tags, executes with the this keyword referring to an instance of the subclass.

The public properties and methods of the class are accessible by ActionScript code in other components, as long as that code "dots down" (for example, myCheckoutAccordion.myAddressForm.firstNameTextInput.text) or reaches up using parentDocument, parentApplication, or Application.application to specify which component the property or method exists on.

Using ActionScript in MXML event handlers

One way to use ActionScript code in a Flex application is to include it within the MXML tag's event handler, as the following example shows:









The executing SWF file for the previous example is shown below:





In this example, you include ActionScript code for the body of the click event handler of the Button control. The MXML compiler takes the attribute click="..." and generates the following event handler method:

public function __myButton_click(event:MouseEvent):void {
textarea1.text='Hello World';
}

When the user clicks the button, this code sets the value of the TextArea control's text property to the String "Hello World." In most cases, you do not need to look at the generated code, but it is useful to understand what happens when you write an inline event handler.

To see the generated code, set the value of the keep-generated-actionscript compiler option to true. The compiler then stores the *.as helper file in the /generated directory, which is a subdirectory of the location of the SWF file.

For more information about events, see Using Events. For more information on using the command-line compilers, see Using the Flex Compilers.

Using ActionScript blocks in MXML files

You use the tag to insert an ActionScript block in an MXML file. ActionScript blocks can contain ActionScript functions and variable declarations used in MXML applications. Code inside tags can also declare constants (with the const statement) and namespaces (with namespace), include ActionScript files (with include), import declarations (with import), and use namespaces (with use namespace).

The tag must be a child of the or other top-level component tag.

Statements and expressions are allowed only if they are wrapped in a function. In addition, you cannot define new classes or interfaces in blocks. Instead, you must place new classes or interfaces in separate AS files and import them.

All ActionScript in the block is added to the enclosing file's class when Flex compiles the application. The following example declares a variable and sets the value of that variable inside a function:









The executing SWF file for the previous example is shown below:

Most statements must be inside functions in an block. However, the following statements can be outside functions:

  • import
  • var
  • include
  • const
  • namespace
  • use namespace

When using an block, you should wrap the contents in a CDATA construct. This prevents the compiler from interpreting the contents of the script block as XML, and allows the ActionScript to be properly generated. Adobe recommends that you write all your open and close tags as the following example shows:





Flex does not parse text in a CDATA construct, which means that you can use XML-parsed characters such as angle brackets (<>) and ampersand (&). For example, the following script that includes a less-than (<) comparison must be in a CDATA construct:








No comments:

Topics