2

Here's an interesting architectural query. I have a piece of code that needs to run on the server (under Node.js) and on the client (in a Flash 10 app written with Actionscript 3). The code is mostly fairly intricate object manipulation, it doesn't make any API calls, and works fine in both contexts.

So far the project is just a demo, so I've been happy to copy and paste the code into both places. But it might be quite interesting to move forward with this.

So how would you do it?

  • I assume there is no easy way to get the Flash SDK (has to build without an IDE) to read and do something useful with a .js file.

  • My only thought is that I could write a code-generator that takes the .js file and places it in an ActionScript wrapper.

Are there any obvious approaches that I've missed?


Just to pre-empt an obvious answer, I know about cross-platform languages like Haxe.

2
  • Does #include check file extension? Commented Apr 24, 2010 at 16:04
  • @drawnonward No, it doesn't. In AS 3.0 the directive is written without the hash sign, though (as opposed to AS 1.0 & 2.0). Commented Apr 24, 2010 at 16:27

1 Answer 1

3

A possible way is using include in your wrapper Actionscript code. Just a quick and very simple test:

package {
    import flash.display.Sprite;
    import flash.text.TextField;

    public class Main extends Sprite {


        private var _alertTxt:TextField;

        include "some.js"

        public function Main() {
            _alertTxt = new TextField();
            _alertTxt.multiline = true;
            _alertTxt.height = 400;
            _alertTxt.width = 400;
            addChild(_alertTxt);
            run();
        }

        public function alert(msg) {
            _alertTxt.text += msg + "\n";
        }
    }
}

some.js

function run() {
    alert("run");
    var obj = {
        a : 'hello',
        b : 4.5,
        c : false
    };
    loop(obj);
}

function loop(obj) {
    for (var field in obj) {
        alert(obj[field]);
    }    
}

To compile from command-line (you might want to add other options):

mxmlc -strict=false Main.as

If you don't set strict to false, it won't compile because of the lack of type declarations.

Sign up to request clarification or add additional context in comments.

3 Comments

Gadzooks, Juan. Didn't know that even existed. I arrived in ActionScript at v.3 when it became a 'serious' development language. And I guess because of that I never found out about include. Perfect solution. Thanks. I'm very pleased it was so simple.
And 30 seconds later, it is working like a charm... Just a FYI in case anyone else happens across this: I don't want to switch off strict for the bulk of my compile. So I'm compiling this wrapped up component into a SWC file for later linking to the final compile. That way the lax checking doesn't allow me to get away with anything I shouldn't elsewhere.
Cool. Yeah, include is not a secret feature but it's not all that publicized either, as its use is generally frowned upon. But I'm glad they didn't remove it from AS 3.0, since it can be quite useful in some scenarios.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.