Please help me in converting a normal Actionscript code to Haxe code

@Sai_Raghava Hello again! Thank you for sharing your file!

I have it up and running here. It was quite simple, but I totally understand that these little differences are not something that would be obvious at first.

I opened your project, and ran a command to test your project (like this one):

openfl test html5

I got these five errors:

Source/Main.hx:43: characters 24-29 : Type not found : as3hx.Compat
Source/Main.hx:44: characters 24-29 : Type not found : as3hx.Compat
Source/Main.hx:46: characters 22-28 : Type not found : Player
Source/Main.hx:66: characters 8-36 : Float should be Int
Source/Main.hx:67: characters 8-36 : Float should be Int

Never fear! Here was the first set:

Type not found : as3hx.Compat

Before

_destinationX = as3hx.Compat.parseInt(stage.stageWidth / 2);
_destinationY = as3hx.Compat.parseInt(stage.stageHeight / 2);

After

_destinationX = Math.round(stage.stageWidth / 2);
_destinationY = Math.round(stage.stageHeight / 2);

I did not include the as3hx haxelib in the project, but personally, I don’t use the as3hx Compat class, anyway. I’ll explain Float and Int in a moment, here’s the last two errors:

Float should be Int

Before

_destinationX = event.stageX;
_destinationY = event.stageY;

After

_destinationX = Std.int(event.stageX);
_destinationY = Std.int(event.stageY);

Float vs Int

You probably know, that Float represents a floating point number (1.0001, 2.2 or 3.0) and Int represents an integer (1, 2 or 3).

Haxe does not allow Float to be converted implicitly to an Int. You have to either decide to keep it as a Float, or to use a method such as Std.int to opt-into potentially losing precision between the types. So in the above cases, event.stageX and event.stageY are Float values.

Although stage.stageWidth and stage.stageHeight are Int values, dividing will cause Haxe to treat the result as a Float, so stage.stageWidth / 2 also is a Float.

The alternative approach here is to change _destinationX and _destinationY to Float values. That is probably easiest, but I wanted to explain this. Last one:

Type not found : Player

You need the generate option to generate Haxe classes for the “Export for ActionScript” class names defined in your SWF. You may also need a -clean build if you have already performed a build using the same SWF file before.

It looks like this in your project:

<library path="Assets/layout.swf" preload="true" generate="true" />

Result

Here’s a little screenshot of the project running:

image

You can use the NPM release of OpenFL as well, if you prefer to use JavaScript, TypeScript or ActionScript :slight_smile:

Have a great day :smile:

3 Likes