1
votes

I'm developing a website which will need to communicate with an AIR application, I develop with javascript and don't know much about actionscript. I'm at the early stages of this and I'm using a trial of Adobe Flash to create some swf files which can communicate with javascript, the code from the Action window is as follows (most of it is bastardised from web examples):

import flash.external.ExternalInterface;

var myText:String = "Example String";

ExternalInterface.addCallback("sendOutput", sendStaticTextFromAS3);
function sendStaticTextFromAS3():void {
    ExternalInterface.call("receiveTextFromAS3", myText);
}

All this is doing is receiving a call from javascript and sending back a string and calling the function which will deal with it (it will become more complex).

My problem is that Flash is going to expire soon, and to future proof the project without having to spend a lot on flash I want to move into FlashDevelop. As I don't need any objects on the stage all I need is to edit actionscript, so FlashDevelop is all I need. I create a new AS3 Project in FlashDevelop and I get given this code:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;

    /**
     * ...
     * @author Will Thomas
     */
    public class Main extends Sprite 
    {

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point
        }

    }

}

I'm guessing Adobe Flash adds this automatically when compiling the swf so I have no idea where my code is supposed to go, except to add the import flash.external.ExternalInterface; at the top?

Any help would be much appreciated, hopefully it's a simple copy and paste thing.

Thanks all.

1

1 Answers

8
votes

What you're looking at is a top-level class document - in this case a .as file describing a class called Main that extends Sprite (all top-level documents that will be compiled into .swfs extend Sprite or MovieClip).

You're right, Flash Professional basically hides this from you (though you can get to the document class if you dig around). But Flash Professional is really a creative tool (for drawing/animating). While it's possible to use it to write ActionScript code, that's not necessarily its strength.

FlashDevelop is a free IDE (that's unfortunately for Windows only) competing against Adobe's Flash Builder. These are geared toward actionscript software development. These IDEs are very handy for the beginner as they will automatically download and install any necessary SDKs, automate the build process, provide project templates, code completion, and debugging, all there in the IDE.

If you'd prefer an IDE to help get you started, then either one of these should work fine (though again, Flash Builder is a commercial tool, I believe Flash Develop is free).

However, do note that you don't need an IDE and can use entirely free Adobe tools to compile .as source code files into .swf files - use the Adobe Flex SDK. It's a command-line compiler that works in Windows, Mac, and Linux, so you'll be using the text editor of your choice to edit your .as files. If this is your preferred workflow, see this tutorial to get you started.

I'd also recommend this excellent article for getting started in flash game development - it's chock full of great info and tutorial links.

As for your question of where your code goes, yes, the import goes at the top with the others, your addCallback statement goes under the "entry point" comment, and the function becomes a function on the Main class (just like the init function - within a class it will need a scope declaration like public, protected or private). Where you place the myText variable depends on when you'll be wanting to change it, but this should do for now:

package 
{
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.external.ExternalInterface;

  /**
   * ...
   * @author Will Thomas
   */
  public class Main extends Sprite 
  {
    public function Main():void 
    {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        // entry point

        ExternalInterface.addCallback("sendOutput", sendStaticTextFromAS3);
    }

    protected function sendStaticTextFromAS3():void {
        var myText:String = "Example String";
        ExternalInterface.call("receiveTextFromAS3", myText);
    }

  }

}

If I recall how ExternalInterface.addCallback works, it looks like what this will do is, when you call sendOutput() from javascript, it will call the sendStaticTextFromAS3 method in ActionScript, which will send the "Example String" back out to the receiveTextFromAS3() JavaScript function - kind of a loopback. You might one one function to handle your javascript-to-as3 communication, while a separate function handles your as3-to-javascript communication:

    private function init(e:Event = null):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        // entry point

        // Setup js-to-as listener
        ExternalInterface.addCallback("sendTextToAS3", receiveTextFromJS);

        // Send example text to js
        sendTextFromAS3("Hello from ActionScript!");
    }

    protected function receiveTextFromJS(text:String):void {
        trace("Received text from js: "+text);
        // TODO: do something with it
    }

    protected function sendTextFromAS3(text:String):void {
        ExternalInterface.call("receiveTextFromAS3", text);
    }

You should also note that there are security restrictions to using addCallback described in the ExternalInterface documentation (your SWF must be embedded on the webpage with allowScriptAccess parameter set).

Hope this helps. =)