2013-06-12 181 views
0

我試圖從JavaScript調用,但得到的瀏覽器下面的錯誤AS3功能:AS3函數調用從JavaScript

對象亙古不支持屬性或方法myCreateFile。

下面是AS3類:

package { 
import flash.display.Sprite; 
    import flash.external.ExternalInterface; 
    import flash.net.FileReference;    
import flash.events.IOErrorEvent; 
import flash.events.Event; 
import flash.system.Security; 

public class CreateDoc extends Sprite { 
    private static const DEFAULT_FILE_NAME:String = "example.txt"; 

    //FileReference Class well will use to save data 
    private var fr:FileReference; 

    public function CreateDoc() 
    { 
     // Register the function for external use. 
     ExternalInterface.addCallback("myCreateFile", myCreateFile); 
     Security.allowDomain("*"); 
    } 

    public function myCreateFile():void 
    { 
     fr = new FileReference(); 
     //open a native save file dialog, using the default file name 
     fr.save("Demo file", DEFAULT_FILE_NAME); 

     fr = null;   
    } 
} 
} 

HTML代碼:

<html> 
<head>  
    <script type="text/javascript" src="swfobject.js"></script> 
    <script type="text/javascript"> 
     try{ 
      var flashvars = {}; 
      var params = {allowscriptaccess:"always", movie:"CreateDoc.swf", wmode:"opaque", menu:"false"}; 
      var attributes = {id:"flashcontent", name:"flashcontent"}; 
      swfobject.embedSWF("CreateDoc.swf", "flashcontent", "800", "600", "10.0.0", "expressInstall.swf", flashvars, params, attributes); 
     } 
     catch(err){ 
      alert(err.message); 
     } 
    </script> 

    <script type="text/javascript"> 
     function doFunction(){ 
      alert('Calling function..'); 
      try{ 
       var myObj = document.getElementById("flashcontent"); 
       myObj.myCreateFile(); 
      } 
      catch(err){ 
       alert(err.message); 
      } 
     } 
    </script> 
</head> 
<body> 
    <div id="flashcontent"> 
    </div> 

    <input id="save file" type="button" value="clickme" onclick="doFunction();" /> 
</body> 

任何想法是錯誤的,當我試圖調用myCreateFile()AS3函數存在在Java腳本中的CreateDoc類?

回答

1

問題是你在三個地方使用了相同的id。在此處更改「flashcontent」: swfobject.embedSWF("CreateDoc.swf", "flashcontent" , ...爲其他內容,例如unique_id,因此它將是:swfobject.embedSWF("CreateDoc.swf", "unique_id" ...。之後,在這裏使用此ID:document.getElementById("flashcontent");也如document.getElementById("unique_id");