在 Flash/ActionScript3 中向外部接口添加回调时的引用问题

发布于 2024-07-25 18:21:40 字数 937 浏览 2 评论 0原文

我有一个方法:myMethod() {},我想让 JavaScript 可以访问它。 我做了一些研究,发现你需要添加一个回调到 ExernalInterface,所以这就是我所做的:

ExternalInterface.addCallback("invokeMyMethod", myMethod);

现在,当我加载带有 flash 的网页时,我收到一个错误:

ReferenceError: Error # 1065: 变量 myMethod 未定义。 在 Main$cinit() 在 global$init()

myMethod 包含在 Main 类中...这是 Main.as 的外观:

package {
   import flash.external.ExternalInterface;
   import flash.events.Event;
   //import a bunch of other things...

   if( ExternalInterface.available ) {
      ExternalInterface.addCallback("invokeMyMethod", myMethod);
   }

   public class Main extends Sprite {
      //A bunch of other methods...

      public function myMethod(str:String):void { 
         //Do something here
      }
   }
}

我不知道如何使 ExernalInterface.addCallback 实现 myMethod 存在...有人有什么想法吗?

谢谢,
马特

I have a method: myMethod() {} that I want to make accessible to javascript. I've done a bit of research and found out you need to add a callback to ExernalInterface, so here's what I have done:

ExternalInterface.addCallback("invokeMyMethod", myMethod);

Now when I load up my web page with the flash on it, I get an error:

ReferenceError: Error #1065: Variable myMethod is not defined.
at Main$cinit()
at global$init()

myMethod is contained within the Main class... here is how Main.as looks:

package {
   import flash.external.ExternalInterface;
   import flash.events.Event;
   //import a bunch of other things...

   if( ExternalInterface.available ) {
      ExternalInterface.addCallback("invokeMyMethod", myMethod);
   }

   public class Main extends Sprite {
      //A bunch of other methods...

      public function myMethod(str:String):void { 
         //Do something here
      }
   }
}

I have no clue how to make ExernalInterface.addCallback realize that myMethod exists... Anyone have any ideas?

Thanks,
Matt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九八野马 2024-08-01 18:21:41

雅各布上面的回答效果很好。 但它产生了其他错误,因为它现在尝试从静态方法访问非静态变量...所以我尝试了这个:

我将:移到

   if( ExternalInterface.available ) {
      ExternalInterface.addCallback("invokeMyMethod", myMethod);
   }

我的主类中,如下所示:

package {
   import flash.external.ExternalInterface;
   import flash.events.Event;
   //import a bunch of other things...     

   public class Main extends Sprite {
      //A bunch of other methods...

      if( ExternalInterface.available ) {
         ExternalInterface.addCallback("invokeMyMethod", myMethod);
      }

      public function myMethod(str:String):void { 
         //Do something here
      }
   }
}

它工作得很好

Jacob's answer above works just fine. But it created other errors because it was now trying to access non-static variables from a static method... So I tried this:

I moved the:

   if( ExternalInterface.available ) {
      ExternalInterface.addCallback("invokeMyMethod", myMethod);
   }

into my Main class, like this:

package {
   import flash.external.ExternalInterface;
   import flash.events.Event;
   //import a bunch of other things...     

   public class Main extends Sprite {
      //A bunch of other methods...

      if( ExternalInterface.available ) {
         ExternalInterface.addCallback("invokeMyMethod", myMethod);
      }

      public function myMethod(str:String):void { 
         //Do something here
      }
   }
}

And it worked fine

绿萝 2024-08-01 18:21:40

您的 myMethod 函数位于 Main 类内部,但您对它的引用(在 if 语句中)不在 Main 类内部。 如果将 myMethod 设为静态,则 addCallback 语句可能如下所示:

ExternalInterface.addCallback("invokeMyMethod", Main.myMethod);

或者,如果您在某处有 Main 实例,则可以编写:

ExternalInterface.addCallback("invokeMyMethod", myObj.myMethod);

Your myMethod function is inside of the Main class, but your reference to it (in the if statement) is not. If you make myMethod static, then your addCallback statement could look like this:

ExternalInterface.addCallback("invokeMyMethod", Main.myMethod);

Or if you have an instance of Main somewhere, you could write:

ExternalInterface.addCallback("invokeMyMethod", myObj.myMethod);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文