2012-02-10 62 views
0

我有一个简单的闪存套接字,用于连接到IRC服务器。它有一个open,closesend方法可用于JS到ExternalInterface,分别用于打开连接,关闭连接和发送消息。当JS收到一条消息时,套接字会调用IRC.io.receive,这个消息被JS解析为一些有用的东西。从JS调用闪存方法时函数不存在

不幸的是,无论何时从JS调用任何Flash方法,它们都会返回“__不是函数”错误。

这里的(冲淡)AS,其中IRC是文档类:

public class IRC extends MovieClip { 
    public static function open(url:String, port:int) {/* code */} 
    public static function close(port:int) {/* code */} 
    public static function send(port:int, message:String) {/* code */} 

    public function IRC() { 
     ExternalInterface.addCallback('send', IRC.send); 
     ExternalInterface.addCallback('open', IRC.open); 
     ExternalInterface.addCallback('close', IRC.close); 
    } 
} 

和HTML/JS:

<html> 
    <head> 
     <script type="text/javascript"> 
      window.IRC = { 
       io: {} 
      }; 
      IRC.io.receive = function(message) {/* code */} 
      IRC.io.send = function(port, str) { 
       document.getElementById('socket').send(port, str); 
      } 
      IRC.io.open = function(url, port) { 
       document.getElementById('socket').open(url, port); 
      } 
      IRC.io.close = function(port) { 
       document.getElementById('socket').close(port); 
      } 
     </script> 
    </head> 
    <body> 
     <!-- ui --> 
     <embed src="socket.swf" quality="high" allowscriptsaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowfullscreen="false" style="display:none;"> 
    </body> 
<html> 

任何与ExternalInterface注册职能的任何调用抛出一个“函数不存在”异常。我做错什么了吗?

回答

1

尝试从您的swf准备好接听电话时发出信号。

例如,在ActionScript:

ExternalInterface.call('initIRQ'); 

而且在你的JavaScript:

function initIRQ() { 
    //Begin communication with swf 
} 
+0

显然,这无法正常工作或 - 闪光灯似乎并不能够调用JS。那么,这可能导致班级工作不正常? – Monchoman45 2012-02-11 00:51:43

+2

检查ExternalInterface是否可用: if(ExternalInterface.available)ExternalInterface.call('initIRQ'); } else { trace(“ExternalInterface unavailable”); }' 如果没有,您可以尝试等待,直到页面完全加载(如果您还没有这样做),然后再尝试访问swf中的函数。 – 2012-02-11 01:16:27

+0

以某种方式修复它 - 它现在起作用。谢谢你的帮助 (: – Monchoman45 2012-02-11 02:24:14