Using the Haxe programming language we can easily create Flash swf files. Haxe's syntax is similar to Actionscript 3.0.
In the example below you will see how to draw a circle, a rectangle, and a custom shape using the drawing api.
You can download the attachment containing the files at the end of the the tutorial.
Note: Setup Haxe and write a simple program.
In the file Drawing.hx write the following Haxe code which contains three functions to draw a circle, a rectangle, and a custom shape.
These three functions are called from init() function.
As you can see the the first line of code shows the package as "com.qon". It means, you have to keep the file Drawing.hx in a directory "\com\qon". You can keep the "com" folder anywhere you want but the path to the file must be ".......\com\qon\Drawing.hx".
package com.qon;
import flash.Lib;
import flash.display.Sprite;
/**
* ...
* @author Qoncious.com
*
*/
class Drawing extends Sprite
{
public function new () // constructor
{
super();
init();
}
private function init():Void
{
drawCircle();
drawRectangle();
drawFreeShape();
}
private function drawCircle():Void
{
graphics.beginFill(0xFF00FF);
graphics.drawCircle(100, 100, 50);
graphics.endFill();
}
private function drawRectangle():Void
{
graphics.beginFill(0xFF0000);
graphics.drawRect(300, 50, 100, 100);
graphics.endFill();
}
private function drawFreeShape():Void
{
graphics.beginFill(0x00FF00);
graphics.moveTo(100, 300);
graphics.lineStyle(1, 0x000000);
graphics.lineTo(100, 350);
graphics.curveTo(150, 275, 300, 325);
graphics.curveTo(150, 375, 100, 300);
graphics.endFill();
}
static function main()
{
Lib.current.addChild(new Drawing());
}
}
See below the drawing.hxml file which contains the compiler options. It says the compiler to create a drawing.swf file. The main class is "com.qon.Drawing". The swf file will be 600px wide and 400px high. The frame rate is 30 frames per second and the background color is white.
The drawing.hxml file and the "com" folder should be in the same directory.
-swf drawing.swf
-main com.qon.Drawing
-swf-header 600:400:30:FFFFFF
Double click on drawing.hxml file to compile the Drawing.hx code. An SWF file will be generated with the drawings.