I am trying to convert some pre-existing html/JavaScript files to Flex. I tried making some research to see if there was any compiler that compiles code from javascript to actionscript. As far as I can see, there are many ways to transform actionscript to javascript, but I couldn't find any for the other way around. Does anyone know if there is a way to do that, or should I just write my own tool?
-
JS to as3 :? usually its the other way around, which there are tools for.Loktar– Loktar2012-06-07 15:48:45 +00:00Commented Jun 7, 2012 at 15:48
-
If you ask me just rewrite what has to be converted.Krasimir– Krasimir2012-06-07 15:52:17 +00:00Commented Jun 7, 2012 at 15:52
-
@KrasimirStefanovTsonev There are too many files, which is why I want to automate the process a little bitMansuro– Mansuro2012-06-07 15:55:13 +00:00Commented Jun 7, 2012 at 15:55
3 Answers
I think you will find this will not work well, as AS3 is a strict-typed language, and Javascript is not. Even if such a compiler exists, your ActionScript will likely have problems from the fact that it would create a bunch of generic object.
In short: ActionScript to Javascript works, because Javascript is more permissive, but a lot of structure is lost. Inheritance, Type, etc.
But to go the other way, there is no way to add back in the structure required by ActionScript.
As one of the commenters mentioned, you may need to do this conversion by hand.
One other idea: the Flash player can talk to Javascript. There may be no need to convert the Javascript to ActionScript, instead just create a few functions to talk to the Flash SWF file through its ExternalInterface class. Just keep the Javascript in Javascript.
Comments
I highly suggest a complete rewrite, it won't take much time since both of them (JS & AS) are ECMA standard languages (correct me if I'm wrong)
But if you really need an automated way, Try:
- Converting your JS to haxe
- Compiling output Haxe to AS
It's the easiest solution on top of my mind.
1 Comment
You're better off writing your own tool. There are so many different ways to write javascript, and including the fact that it's untyped, it would be difficult to write a be-all end-all solution that converts everything. If it's a library, it might make more sense to convert opcodes rather than code-to-code, but so much of it depends on how the javascript was written.
It's going to be a pain, that's for sure.
- js is untyped, actionscript is "typed". I guess everything could be an object and it would work well
- js function scoping is handled differently.
- anonymous function calling in actionscript is generally frowned upon, and is a detriment to performance
- stuff like setTimeOut in actionscript is also frowned upon, enter_frame and timer are probably more useful
- garbage collection in actionscript is handled a bit differently -- as is event dispatching