在 Flash/ActionScript3 中向外部接口添加回调时的引用问题
我有一个方法: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
雅各布上面的回答效果很好。 但它产生了其他错误,因为它现在尝试从静态方法访问非静态变量...所以我尝试了这个:
我将:移到
我的主类中,如下所示:
它工作得很好
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:
into my Main class, like this:
And it worked fine
您的 myMethod 函数位于 Main 类内部,但您对它的引用(在 if 语句中)不在 Main 类内部。 如果将 myMethod 设为静态,则 addCallback 语句可能如下所示:
或者,如果您在某处有 Main 实例,则可以编写:
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:
Or if you have an instance of Main somewhere, you could write: