Flash:HaXe and Flash 9
From GMpedia.org Wiki
This tutorial assumes you're jumping into haXe anew but have some idea of the syntax and how to program.
Here is a compile.hxml for Flash 9:
-swf haxegame.swf -swf-version 9 -swf-header 300:300:30:000000 -main Main
You will probably want to add
-debug
so that you can get additional stack trace information when runtime errors show up.
Here is a basic Main.hx:
class Main
{
public static function main()
{
}
}
This will compile, but you'll get a blank screen. Now the question is, how to start adding interesting stuff?
We can start with a debugging displayer, DebugDisp.hx:
import flash.text.TextField;
class DebugDisp
{
public var debugmsg : TextField;
private var colors : Array <Dynamic>;
private var cycle : Int;
private var datalog : String;
public function new()
{
debugmsg = new TextField();
debugmsg.x = 4;
debugmsg.y = 4;
debugmsg.width = 300-4;
debugmsg.height = 300-4;
debugmsg.textColor = 0xFFFFFF;
debugmsg.text = "hi";
datalog = "";
cycle = 0;
}
public function up(textval : String)
// Update with new text.
{
debugmsg.text = textval;
}
public function upcycle(textval : String)
// Update with new text and cycle colors.
{
cycle+=1;
if (cycle>=colors.length) {
cycle = 0;
}
debugmsg.text = textval;
debugmsg.textColor = colors[cycle];
}
public function color(color : Dynamic)
// Set a color. Overwritten by colorcycles.
{
debugmsg.textColor = color;
}
public function colorcycle(newcolors : Array <Dynamic>)
// Set a cycle of colors, with a new one each time upcycle is called.
{
colors = newcolors;
cycle = 0;
debugmsg.textColor = colors[cycle];
}
public function log(textval : String)
// Add to a persistent log which can be printed later.
{
datalog = datalog + textval + "\n";
}
public function uplog()
{
debugmsg.text = datalog;
}
}
This debugger does several useful things:
- Uses a Textfield to display text
- Can be given arbitrary strings with the up() function
- Can be given colors and cycled through colors with color(), colorcycle(), and upcycle()
- Can write a multiple-entry log for cases where you want to aggregate data with log() and uplog()
To use the debugger we have to change Main.hx a little to include the class:
import DebugDisp;
import flash.Lib;
class Main
{
public static function main()
{
var mydebug : DebugDisp = new DebugDisp();
Lib.current.addChild(mydebug.debugmsg);
}
}
If you aren't familiar with how Flash works: it has a global display object, the stage, represented in haXe by the Lib.current instance. You add things to the display with addChild; additional functions exist to change the order or remove children. Some display classes can have their own children, allowing you to compose complex, well-organized trees of graphics.
To work with the Flash libraries more you should consult the Flex 2 reference:
(more should be added on the use of events, loaders, and other neat Flash things)

