This is an archive of the old community forums, please visit the new site at openfl.org or the new community at community.openfl.org.

AS3 Developer Guide

Cheat Sheet

It is easy to transition to Haxe from ActionScript. In fact, the Haxe programming language was created by Flash developers. It started with MTASC, the open-source ActionScript 2 compiler, and evolved to the Haxe we love and know today. Haxe supports many advanced features ActionScript wishes it supported.

This is a "cheat sheet" with a list of areas where Haxe and Actionscript 3 differ from one another. Places where the languages are the same have not been included. The column on the left is AS3, and the column on the right is Haxe.

Basic Types

 
  1. Boolean
  2. int
  3. Number
  4. Object
  5. void
  6. Array
  7. Vector.<String>
  1. Bool
  2. Int
  3. Float
  4. Dynamic
  5. Void
  6. Array<Dynamic>
  7. Array<String>

Package Declarations

 
  1. package com.example.myapplication {
  2.  
  3. }
  1. package com.example.myapplication;

Defining a Class

 
  1. public class MyClass {
  2.  
  3. public function MyClass () {
  4.  
  5.  
  6. }
  7.  
  8. }
  1. class MyClass {
  2.  
  3. public function new () {
  4.  
  5.  
  6. }
  7.  
  8. }

Loops

 
  1. for (var i:uint = 0; i < 100; i++) {
  2.  
  3. }
  4.  
  5. for each (var value:String in items) {
  6.  
  7. }
  8.  
  9. for (var propertyName:String in object) {
  10.  
  11. }
  1. for (i in 0...100) {
  2.  
  3. }
  4.  
  5. for (value in items) {
  6.  
  7. }
  8.  
  9. var fields = Reflect.fields (object);
  10. for (propertyName in fields) {
  11.  
  12. }

Switch Statements

 
  1. switch (value) {
  2.  
  3. case 1:
  4. trace ("Equal to 1");
  5. break;
  6.  
  7. default:
  8. trace ("Not equal to 1");
  9. break;
  10.  
  11. }
  1. switch (value) {
  2.  
  3. case 1:
  4. trace ("Equal to 1");
  5.  
  6. default:
  7. trace ("Not equal to 1");
  8.  
  9. }

Type Inference

 
  1. var hi = "Hello World";
  2.  
  3. // type is Object
  4. // fails to compile in strict mode
  1. var hi = "Hello World";
  2.  
  3. // type is String
  4. // even works for code completion

Type Casting

 
  1. var car:Car = vehicle as Car;
  2.  
  3. var toString:String = String (10);
  4. var toNumber:Number = Number ("10");
  5. var toInteger:int = int (10.1);
  1. var car:Car = cast vehicle;
  2.  
  3. // or for a safe cast:
  4.  
  5. var car = cast (vehicle, Car);
  6.  
  7. var toString = Std.string (10);
  8. var toNumber = Std.parseFloat ("10");
  9. var toInteger = Std.int (10.1);

Type Details

 
  1. if (vehicle is Car) {
  2.  
  3. }
  4.  
  5. import flash.utils.getDefinitionByName;
  6. import flash.utils.getQualifiedClassName;
  7.  
  8. name = getQualifiedClassName (vehicle);
  9. type = Class (getDefinitionByName (name);
  1. if (Std.is (vehicle, Car)) {
  2.  
  3. }
  4.  
  5. type = Type.getClass (vehicle);
  6. name = Type.getClassName (type);

Checking for Null

 
  1. if (object == null) {
  2.  
  3. }
  4.  
  5. if (!object) {
  6.  
  7. }
  1. if (object == null) {
  2.  
  3. }

Hash Tables

 
  1. var table:Object = new Object ();
  2. table["key"] = 100;
  3.  
  4. trace (table.hasOwnProperty ("key"));
  5.  
  6. for (var key:Object in table) {
  7.  
  8. trace (key + " = " + table[key]);
  9.  
  10. }
  11.  
  12. delete table["key"];
  1. var table = new Map<String,Int> ();
  2. table.set ("key", 100);
  3.  
  4. trace (table.exists ("key"));
  5.  
  6. for (key in table.keys ()) {
  7.  
  8. trace (key + " = " + table.get (key));
  9.  
  10. }
  11.  
  12. table.remove ("key");

Rest Parameters

 
  1. function test (...params):void {
  2.  
  3. }
  4.  
  5. test (1, 2, 3);
  1. function test (params:Array<Dynamic>) {
  2.  
  3. }
  4.  
  5. Reflect.makeVarArgs (test) (1, 2, 3);

Reflection

 
  1. var foo = object["foo"];
  2.  
  3. bar.apply (this, [ "hi" ]);
  1. var foo = Reflect.field (object, "foo");
  2.  
  3. Reflect.callMethod (this, bar, [ "hi" ]);

Constants

 
  1. private const gravity:Number = 9.8;
  1. inline private static var gravity = 9.8;

Function Types

 
  1. function hello (msg:String):void {
  2.  
  3. }
  4.  
  5. var type:Function = hello;
  1. function hello (msg:String):Void {
  2.  
  3. }
  4.  
  5. var type:String->Void = hello;
  6.  
  7. // can also use Dynamic

Getters and Setters

 
  1. function get x ():Number {
  2.  
  3. return _x;
  4.  
  5. }
  6.  
  7. function set x (value:Number):void {
  8.  
  9. _x = value;
  10.  
  11. }
  1. public var x (get, set):Float;
  2.  
  3. function get_x ():Float {
  4.  
  5. return _x;
  6.  
  7. }
  8.  
  9. function set_x (value:Float):Float {
  10.  
  11. return _x = value;
  12.  
  13. }

Read-Only Properties

 
  1. function get x ():Float {
  2.  
  3. return _x;
  4.  
  5. }
  1. public var x (default, null):Float;
  2.  
  3. // null allows private access
  4. // `never` would restrict all access

Missing Features

Haxe does not currently support custom namespaces, and methods do not provide an arguments property.

Additional Features

In addition to most of the features of Actionscript 3, Haxe includes support for enums, type parameters (generics), structures, typedefs, custom iterators, conditional compilation, inlining and more.

Additional Packages

Haxe comes with a standard set of classes which are specific to each platform. If you are targeting Flash, you can use regular classes from the flash.* package. If you are targeting Javascript, you can use DOM and browser specific code using the js.* package.

To read files, directories or perform other common system functions, you can access classes in the cpp.* or neko.* packages, depending on whether you use C++ or Neko when compiling for the desktop.

OpenFL provides a consistent subset of theĀ flash.* package, for C++ platforms, Neko and HTML5. OpenFL has some additional features, which will usually be under the openfl.* package.

Miscellaneous Notes

In Haxe, class packages must be lower-case, and class names must begin with a capital letter. This convention is looser in Actionscript 3. You must also import classes by name, so don't use wildcards.

The private namespace in Haxe is similar to the protected namespace in Actionscript 3.