1. Overview
1) Runtime
ActionScript Virtual Machine (AVM) – ActionScript3 -> Compiler -> Bytecode instruction set -> AVM. (Open source project: Tamarin by Molliza Foundation).
AVM1: ActionScript1, ActionScript2 (< Flash player 9)
AVM2: ActionScript3 (>= Flash player 9)
a) Adobe Air - supporting HTML, Javascript and Actionscript (Power by Webkit and AVM)
b) Adobe Flash Player – including AVM
*. Active X based – Internet Explorer
*. Plugin based – Firefox, Opera, Safari …
*. Built-in support – Google Chrome (type “about:plugins” to switch the system plugin version)
c) Adobe Flash Lite – a lightweight version of Flash Player, low hardware requirement
2) Flex SDK
Built on the top of Air and Flash player core APIs, supplying UI component, data access componet and so forth.
3) Flash Builder – IDE
Built on the top of Flex SDK, Eclipse based.
2. Flex Tools
1) SDK Tools
a) Flash player
mxmlc - Actionscript compiler, accept .mxml or .as files (using flex-conifg.xml), output .swf file
b) Air
amxmlc- Air application builder (using framewords/air-config.xml instead of flex-conifg.xml), calling mxmlc eventually.
adl - (Air Debug Launcher), run Air application from XML.
e.g.: adl HelloWorld-app.xml
adt -(Air Developmemt Tool)
-certificate (generated self-signed certificates): adt –certificate -cn MyCNName 1024-RSA(2048-RSA) MyCertName.pfx MyCertPassword
-package (packing files as Air installation package): adt -package -storetype pkcs12 -keystore MyCertName.pfx HelloWorld.air
HelloWorld-app.xml HelloWorld.swf
The package actually builds a .air Air application (which is needed the Air runtime to work). When it runs, unzip the content files (app.xml and .swf) and generate an OS-specified execution file. The execution will call the Air runtime and pass it with the content files.
[Sample Code: Adobe Air HelloWorld]
2) Debug Tools
a) Flash player debugger: Latest Flash player debugger, Archived Flash player
b) How to use Flash player debugger
-mm.cfg: Flash player debugger configure file, placed at the user’s home (e.g. c:\Users\username\) . How to configure it
c) Firefox – Firebug extenstion: Flashbug, FlashFirebug
3. Flex Development
http://help.adobe.com/en_US/flex/using/index.html
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/index.html
- Component set
*. MX (introduced with Flex 3)
*. Spark (introduced with Flex 4)
- Debug output
*. trace(“messages”);
*. mx.controls.Alert.show(“messages”, “title”);
- package
package equals C#’s namespace, it can be contained in difference .swc file, but must not be nested (class must not be nested either, but function can be nested). Packages in ActionScript 3.0 are implemented with namespaces.
package [package name] { code…}
package code.subcode {code …} (mimic nested namespace)
ActionScript 3.0 allows you to include multiple classes in one source file, but only one class in each file can be made available to code that is external to that file. In other words, only one class in each file can be declared inside a package declaration. You must declare any additional classes outside your package definition, which makes those classes invisible to code outside that source file. The name of the class declared inside the package definition must match the name of the source file.
In ActionScript 3.0, you use the statement to declare a package, which means that you can also declare variables, functions, and namespaces at the top level of a package. You can even include executable statements at the top level of a package. If you do declare variables, functions, or namespaces at the top level of a package, the only attributes available at that level are public and internal, and only one package-level declaration per file can use the public attribute, whether that declaration is a class, variable, function, or namespace.
- namespace
Think of thepublic,private,protected,andinternalaccess control specifiers as built-in namespaces. Definitions that can be placed into namespaces include functions, variables, and constants (you cannot place a class into a custom namespace).
a). Define the namespace. If you omit the URI, the compiler will create a unique internal identification string in place of the URI.
namespace version1 namespace flash_proxy = "http://www.adobe.com/flash/proxy";
b). apply your namespace by using it instead of an access control specifier in a property or method declaration
version1 var function myFunction(): void
c). use it. Once you open a namespace withuse namespace, it remains open throughout the block of code in which it was opened. There is no way to explicitly close a namespace.
use namespace version1; // open the namespace myFunction();
or
version1::myFunction();
- Include and Import
*. include “.as file path” or <fx:script source=”.as file path”/>: include statements or function definitions only. The result is the same as if the code were entered there directly.
*. import: import ActionScript class definitions. Just like using built-in ActionScript classes. Including package and class name (class full name). Top-level classes (in a package without a package name) no need to import before being used.
import flash.display.MovieClip
import flash.display.*
-access specifier
ActionScript 3.0, supports not only classes at the top level of a package, but also variables, functions, and even statements. However, that only two access specifiers,publicandinternal , are allowed at the top level of a package. ActionScript 3.0 supports neither nested nor private classes.
*. class: public, internal (default value), final and dynamic are also available for class.
*. class member: namespace, private, protected, public, internal (default value)
- final & dynamic & override
*. final: Classes with the finalattribute can’t be extended. Method with the final attribute can’t be override. (only instance methods can have the override and final modifier)
*. dynamic: A dynamic class defines an object that can be altered at run time by adding or changing properties and methods. You cannot add a type annotation to a property that you add in this manner (type checking is done at run time). Also, do not have access to any private properties or methods of the Protean class.
*. override: You can use the overrideattribute to redefine an inherited instance method.
(static methods in ActionScript 3.0 are not inherited.)
- variable scope
*. global scope
*. function scope
*. class scope
There is no a block scope. This is because of a technique called hoisting:
Hoisting: the compiler moves all variable declarations to the top of the function, however, will not hoist any assignment statements. This means that you can assign values to variables even before they are declared.
- Object
Object should be a dynamic class.
// Use new statement and add properties.
var myObject:Object = new Object();
myObject.propA = 1;
myObject.propB = 2;
myObject.propC = 3;
// Assign literal directly.
var myObject:Object = {propA:1, propB:2, propC:3};
- Function
Functions in ActionScript 3.0 are objects. Every function has a read-only property named lengththat stores the number of parameters defined for the function.
Functions can be nested inside another function.
function getNameAndVersion():String
{
function getVersion():String
{
return "10";
}
function getProductName():String
{
return "Flash Player";
}
return (getProductName() + " " + getVersion());
}
trace(getNameAndVersion()); // Flash Player 10
*. function statement
function traceParameter(aParam:String)
{
trace(aParam);
}
traceParameter("hello"); // hello
a delete operation to the function (delete traceParameter) won’t have effect.
Function statements are available before they are defined
*. function expression (anonymous functions)
var traceParameter:Function = function(aParam:String)
{
trace(aParam);
};
traceParameter("hello"); // hello
the function expression is attache to the variable (traceParameter), if the variable is deleted (delete traceParameter), the function express will be garbage collected.
Function expressions are not available before they are defined.
Can also use the following way to call the class’s expression function.
myExample["methodLiteral"]();
- Function parameters
In ActionScript 3.0, all arguments are passed by reference, because all values are stored as objects. However, objects that belong to the primitive data types, which includes Boolean, Number, int, uint, and String, have special operators that make them behave as if they were passed by value.
*. Default parameter values (optional parameter)
function defaultValues(x:int, y:int = 3, z:int = 5):void
{
trace(x, y, z);
}
defaultValues(1); // 1 3 5
*. The arguments object
The argumentsobject is not available if any parameter is named arguments
or if you use the … (rest) parameter.
a) arguments.callee: property provides a reference to the function itself.
b) arguments.length: property shows the number of the actually passed parameters (not the number of parameters defined for the function).
*. The … (rest) parameter
the … (rest) must be the last parameter.
function traceArgArray(x:int, ... args):void
{
for (var i:uint = 0; i < args.length; i++)
{
trace(args[i]);
}
}
traceArgArray(1, 2, 3);
// output:
// 2
// 3
- this & super
this: reference to the current class instance.
super: reference to the superclass instance
Both the thisreference and the superreference have meaning only within the context of an instance method.
If you decide to use bothsuper()and superin the same constructor body, be sure to call super()first.
- embed resource
[Embed(source="sound1.mp3")] public var soundCls:Class; var mySound:SoundAsset = new soundCls() as SoundAsset; var sndChannel:SoundChannel = mySound.play();