2 Replies Latest reply on Jan 15, 2009 8:20 PM by MrWindupBird

    Calling Actionscript functions from HTML in Air?

    MrWindupBird
      Arg... I feel like I am soo close - but something is not quite right...

      I am stuck on the communcation from Javascript back to Actionscript in an Adobe Air 1.5 app... (not flex).

      here is my Main():
      quote:


      public class Main extends Sprite {

      public var _as3Var:String = "testing";
      public function as3Function():void
      {
      trace("as3Function called from Javascript");
      }

      public function Main() {
      NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, onInvoke);
      }

      protected function onInvoke(e:InvokeEvent):void {
      NativeApplication.nativeApplication.removeEventListener(InvokeEvent.INVOKE, onInvoke );
      var app = new App();
      addChild(app);
      app.init(new ExternalContainer(), e.currentDirectory, e.arguments);
      }
      }
      }



      Now, I simply want to either access _as3Var or as3Function() from my AppSandbox javascript... My HTMLLoader looks like this:
      quote:


      {
      _html = new HTMLLoader();
      _html.useCache = false;
      _html.runtimeApplicationDomain = ApplicationDomain.currentDomain;
      _html.load(new URLRequest("sandbox/AirRoot.html"));
      }



      And this is my javascript snippit:
      quote:


      Exposed.testAs3 = function()
      {
      air.trace("Exposed.testAs3 called"); /* this works great */
      air.trace("runtimeVersion:"); /* this works great */
      air.trace(air.NativeApplication.nativeApplication.runtimeVersion); /* this works great */

      air.trace("seeing if I can get to AS3 params..."); /* this works great */
      air.NativeApplication.nativeApplication.as3Function(); /* this produces an exception: TypeError: Value undefined does not allow function calls. */
      }




      What I am doing wrong?
        • 1. Re: Calling Actionscript functions from HTML in Air?
          Joe ... Ward Level 4
          as3Function and _as3Var are members of Main, not the nativeApplication object.

          How you can reference your Main object depends on the relationship between Main and the HTMLLoader running the javascript. For example, if _html is a child of Main, you could call as3Function like this: window.htmlLoader.parent.as3Function().

          If _html is not a child of Main, then you can either create a static reference to Main in the Main class constructor:

          public static var mainInstance:Main;
          public function Main() {
          mainInstance = this;
          //...
          }

          Or create a reference to Main in your JavaScript object when you create _html. If _html is created inside Main (so that this is the Main instance), then the following would work:

          _html.window.mainInstance = this;

          You could then call as3Function like this:
          window.mainInstance.as3Function();
          • 2. Re: Calling Actionscript functions from HTML in Air?
            MrWindupBird Level 1
            Thanks... makes sense now. I am not sure why I was fixated on the NativeApplication object. It is now clear that the HTMLLoader.window object is the proxy between the Actionscript and Javascript world in AIR - and also that window.htmlLoader is the proxy object between Javascript and Actionscript.