Auroratide

Coding + Storytelling

AS3 to Haxe Conversion

Game Dev

Topics
  • flash
  • actionscript
  • haxe
  • conversion
  • guide

Haxe shares many similarities with the Actionscript 3 (AS3) language, and therefore AS3 developers like myself can pick it up rather easily. There are, of course, some immediate differences that should be noted if wanting to write Haxe code, and I attempt to note the most important ones here. This post borrows and augments the information found at OpenFL's site.

Please note that this is by no means a complete guide to Haxe! One of the most glaring differences between AS3 and Haxe is Haxe's incredibly expansive functionality. For instance, Haxe allows for generic type parameters whereas AS3 does not; such a feature can be found in Java generics and C++ templates. Haxe furthermore allows for Enum structures, inlining, type abstraction, and much more. I will explore such things in later posts, but you can get a head start by peeking at Haxe's manual.

Below, though, you can find some quick conversions from AS3 code to Haxe, along with some notes about how Haxe compares with Java or C++ for those more familiar with those languages.

  • Both Number and Float are IEEE double-precision numbers and use 64 bits
  • Many sources will compare Haxe's Dynamic construct to AS3 or Java's Object class. However, I believe that Dynamic is more similar to the * (untyped) in AS3. Dynamic removes compile-time type checking just like the * typing in AS3.
  • We essentially replace Vectors with Arrays. In Java, this is the same as ArrayList<T>, and in C++, we use vector<T>.
  1. int
  2. uint
  3. Number
  4. Boolean
  5. void
  6. Object
  7. Array
  8. Vector.<T>
  1. Int
  2. UInt
  3. Float
  4. Bool
  5. Void
  6. Dynamic (?)
  7. Array<Dynamic>
  8. Array<T>
  • Constructors in Haxe are now named new rather than the class name
  • Do NOT include public before class in Haxe
package com.newprogrammer {
	public class Circle extends Shape implements Drawable { 
		public function Circle() { } 
	} 
}
package com.newprogrammer; 
 
class Circle extends Shape implements Drawable {
	public function new() { } 
}
  • There is no const keyword in Haxe. Instead we use inline var for constants.
  • I will discuss accessors more in detail in a future post.
public class Circle {
	public static const PI: Number = 3.1415;

	private var _radius: Number; 

	public function get radius(): Number{ 
		return _radius; 
	} 
	public function set radius(value: Number): void { 
		if(value < 0)
			throw "Error";
		_radius = value;
	} 
}
class Circle { 
	public static inline var PI: Float = 3.1415;
	public var radius(default, set): Float;

	public function set_radius(value: Float): Float { 
		if(value < 0) 
			throw "Error"; 
		return radius = value; 
	} 
}
  • for loops do not support the traditional C syntax like AS3 or Java do
  • for loops only work on iterable entities
  • Haxe uses the "in" keyword like Java uses the colon (:) operator
for(var i: int = 0; i < 100; ++i) { } 
 
for each(var v: T in items) { }
for(i in 0...100) { } 
 
for(v in items) { }
  • Haxe always has an implicit break statement for each case
  • Haxe allows the use of pattern matching in case expressions (not pictured above), making switch statements overall very powerful
switch(value) { 
	case 1: 
		trace("Value is 1"); 
		break; 
	case 2: 
		trace("Value is 2"); 
		break; 
	default: 
		trace("Default reached"); 
}
switch(value) { 
	case 1: 
		trace("Value is 1"); 
	case 2: 
		trace("Value is 2"); 
	default: 
		trace("Default reached"); 
}
  • Haxe has a new Map structure that handles hash tables more elegantly
var table: Object = new Object(); 
table["key"] = 1; 
 
if(table.hasOwnProperty("key")) 
	trace(table["key"]);
 
for(var key: Object in table) { } 
 
delete table["key"];
var table = new Map<String, Int>();
table["key"] = 1;
 
if (table.exists("key")) 
	trace(table["key"]);
 
for (key in table.keys()) { } 
 
table.remove("key");

Some other notable differences include:

  • Haxe allows for type inference. In AS3, not giving a variable a type will cause it to be untyped. Haxe will actually guess what the type of a value is if not given.
  • Rather than having a generic Function class, Haxe has its own way of assigning types to functions. You can read about it here.
  • Haxe's private access specifier is actually equivalent to protected in other languages, meaning that child classes can access private members of their parents. Because of this, there is no notion of a truly private variable.

OpenFL's page specifies even more differences regarding casting and reflection. In future posts, I will begin looking at some of Haxe's features in more depth.

And, since it is 25 December, Merry Christmas to all readers!