javascript和Flash之间的通信



我已经添加了:

ExternalInterface.addCallback('sendToActionScript', setKeyboardFocus);
ExternalInterface.call("setFocus", 'pathfinder');

在我的主类的init()函数中。

在html中我有这个:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
    <title>pathFinder</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css" media="screen">
    html, body { height:100%; background-color: #333333;}
    body { margin:0; padding:0; overflow:hidden; }
    #flashContent { width:100%; height:100%; }
    </style>
 <script type="text/javascript">

 function getFlashMovie(movieName) {
 var isIE = navigator.appName.indexOf("Microsoft") != -1;
 return (isIE) ? window[movieName] : document[movieName];
 }

function callToActionscript(str) 
{  
var fm = getFlashMovie("pathfinder");
fm.sendToActionScript(str);
}
function setFocus(id){ 
 var f =  document.getElementById(id);
 f.focus();
 callToActionscript('test') 
 } 

</script>
</head>
<body>

    <div id="flashContent" align='center'>
<table width="100%" height="100%" border="0" align="center" cellpadding="0"  
cellspacing="0">
<tr>
  <td align="center" valign="middle" bgcolor="#333333"><table width="1050" border="0"       
cellpadding="0" cellspacing="0">
    <tr>
      <td>
    <div align="center">
<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" 
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#versio
 n=6,0,0,0" 
WIDTH="1050" 
HEIGHT="600" 
id="pathfinder" 
ALIGN="middle">
<PARAM NAME=movie VALUE="pathFinder.swf"> 
<PARAM NAME=quality VALUE=high> 
<PARAM NAME=bgcolor VALUE=#333333> 
<EMBED src="pathFinder.swf" quality=high bgcolor=#333333 WIDTH="1050" HEIGHT="600" 
NAME="pathfinder" ALIGN="middle" TYPE="application/x-shockwave-flash" 
PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer" allowScriptAccess="always"> 
</EMBED> </OBJECT></div></td></tr></td></tr></table>
    </div>
</body></html>

在主阶段,我有一个动态文本字段实例"test_txt"来检查是否调用了该函数。

所以在ExternalInterface代码之后,我有:

private function setKeyboardFocus(str:String):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN,checkKeysDown);
stage.addEventListener(KeyboardEvent.KEY_UP,checkKeysUp)
test_txt.text = str
}

问题是flash无法获得键盘焦点(KeyboardEvent的事件列表从未添加),函数setKeyboardFocus从未被调用。

有什么帮助吗?

根据我找到的一个源,您需要在两个位置添加allowScriptAccess="always"

  1. <EMBED ... allowScriptAccess="always"> </EMBED>块中
  2. 您还需要<PARAM NAME="allowScriptAccess" VALUE="always" >和其他PARAM块

验证是否将fm分配给对象或嵌入组件。您应该能够使用Chrome或Firefox中的javascript调试工具来验证这一点。我认为你错在这里。这是一个完全非标准的html包装器,从我自己看到的情况来看,但在大多数情况下,它似乎是有序的。然而,有一件事是错误的,即Object标签和信息将应用于IE,Embed标签信息将适用于使用Netscape插件的浏览器(Firefox……除了IE,其他都适用)。此外,我在Embed元素上没有看到id,我认为你也需要像对待Object那样给它一个id,我不确定如果你使用相同的id,你是否会得到javascript错误,我可能会称之为pathFinderE或类似的东西,然后修改这个方法:

 function getFlashMovie(movieName) {
 var isIE = navigator.appName.indexOf("Microsoft") != -1;
 return (isIE) ? window[movieName] : document[movieName + "E"];
 }

最新更新