簡體   English   中英

javascript和Flash之間的通信

[英]Communication between javascript and 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包裝,來自我自己看到的,但在大多數情況下,它看起來你的東西是有序的。 那將是錯誤的,雖然有一件事是,對象標簽和信息將適用於IE瀏覽器,嵌入標簽信息將被應用為使用Netscape瀏覽器插件(火狐......很好,但一切IE)瀏覽器。 另外我沒有看到嵌入元素ID,我想你也需要給這一個ID喜歡你的對象做了,我不知道,如果你使用完全相同的ID,你會得到的JavaScript錯誤,我可能會稱之為pathFinderE或類似的東西,然后修改此方法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM