2011-06-01 50 views
6

我试图在使用ExternalInterface.addCallback API的动作脚本中调用一个函数,但我似乎无法让它工作。下面是我有:通过Javascript访问ActionScript函数

的ActionScript:

//MyClass.as 
package { 

    import flash.display.Sprite; 
    import flash.external.ExternalInterface; 

    public class MyClass extends Sprite 
    { 
     public function MyClass() 
     { 
      ExternalInterface.addCallback('getStringJS', getStringAS); 
     } 

     public function getStringAS():String 
     { 
      return "Hello World!"; 
     } 
    } 
} 

注:我使用的是柔性mxmlc编译如果该事项编译为swf这一点。

HTML/JavaScript的:

<!doctype html> 
<html> 
    <head> 
     <title>User Identification</title> 
    <head> 
    <body> 
     <object id="MyClass" name="MyClass" type="application/x-shockwave-flash" data="MyClass.swf" width="1" height="1"> 
      <param name="movie" value="MyClass.swf"> 
      <embed src="MyClass.swf" width="1" height="1"> 
     </object> 
     <script type="text/javascript"> 
      var flash = document.getElementById("MyClass"); 
      var str = flash.getStringJS(); 
      alert(str); 
     </script> 
    </body> 
</html> 

我得到的错误是:

Uncaught TypeError: Object #<HTMLObjectElement> has no method 'getStringJS' 

我也尝试添加在情况下SWF文件是不加载超时,但我这种方法也没有任何成功。

有什么想法?

干杯,
迈克

回答

5

我想通了。通过ExternalInterface.call发信号通知javascipt的关键方法,因此我们确信swf已加载。最“通用”的方式来做到这一点如下:

MyClass.as

//MyClass.as 
package { 

    import flash.display.Sprite; 
    import flash.external.ExternalInterface; 

    public class MyClass extends Sprite 
    { 
     public function MyClass() 
     { 
      ExternalInterface.addCallback('getStringJS', getStringAS); 
      if (ExternalInterface.available) { 
       ExternalInterface.call("isConnectedFlex"); 
      } 
     } 

     public function getStringAS():String 
     { 
      return "Hello World!"; 
     } 
    } 
} 

的index.html

<!doctype html> 
<html> 
    <head> 
     <title>User Identification</title> 
    <head> 
    <body> 
     <object id="MyClass" name="MyClass" type="application/x-shockwave-flash" data="MyClass.swf" width="1" height="1"> 
      <param name="movie" value="MyClass.swf"> 
      <embed src="MyClass.swf" width="1" height="1"> 
     </object> 
     <script type="text/javascript"> 

      var flash = document.getElementById("MyClass"); 

      function isConnectedFlex() { 
       var str = flash.getStringJS(); 
       alert(str); 
      } 


     </script> 
    </body> 
</html> 
+0

不错的例子。作品仍在2017年。对于其他人,还可以在此处找到其他一些其他信息:http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7cb1.html – Aleks 2017-02-09 13:40:57

2

我认为这个问题是没有被加载闪存的问题。我尝试使用window.onload事件代码和它的工作对我来说:

闪光灯一样的...

HTML/JS:

<!doctype html> 
<html> 
    <head> 
     <title>User Identification</title> 
    <head> 
    <body> 

     <object id="MyClass" name="MyClass" type="application/x-shockwave-flash" data="MyClass.swf" width="1" height="1"> 
      <param name="movie" value="MyClass.swf"> 
      <embed src="MyClass.swf" width="1" height="1"> 
     </object> 

     <script> 

      window.onload = function() { 

       var flash = document.getElementById("MyClass"); 
       var test = flash.getStringJS("test"); 
       alert(test); //pops up with "Hello World!" on Firefox 

      }; 

     </script> 
    </body> 
</html> 

这是否帮助?

+0

是啊,这让我意识到我真的需要学习来检查我的在其他浏览器工作哈哈。它适用于Firefox和IE,但不适用于Chrome。无论哪种方式,我仍然需要一个更普遍接受的方法。有什么想法吗? – Swift 2011-06-01 23:15:31

+0

我明白了。你可能想看看这个链接:[问题访问外部接口暴露的方法在谷歌浏览器](http://stackoverflow.com/questions/1436722/problem-accessing-externalinterface-exposed-method-in -google-chrome) – Ian 2011-06-01 23:24:00