2017-03-14 94 views
0

我有一個acrionscript swf文件的另一個問題。我正在閱讀CSV文件中的一些文本。我創建fla文件,當我在動畫編輯器中測試它時,它可以工作,並且可以將CSV文件中的所有文本看到TextField中。如果我導出.swf並使用Adobe Flash Player 11.4 r402運行它,則無法看到CSV文件中的文本。我創建了我寫了這個代碼一個黑影照片製成了這個FLA文件:ActionScript 3.0 .fla文件工作,.swf文件導出不工作

import flash.net.URLRequest; 
import flash.net.URLLoader; 
import flash.events.Event; 
import flash.events.IOErrorEvent; 
import flash.filesystem.*; 
import flash.text.TextField; 

var pathToFile:String = File.applicationDirectory.resolvePath('ex.csv').nativePath; 

var requestedFile:URLRequest = new URLRequest(pathToFile); 

var myTextField:TextField = new TextField(); 

myTextField.autoSize = TextFieldAutoSize.LEFT; 

var documentLoader = new URLLoader(); 

documentLoader.addEventListener(Event.COMPLETE, onload); 

documentLoader.load(requestedFile); 

function onload(evt:Event):void 
{ 
    myTextField.text = evt.target.data; 
    addChild(myTextField); 
} 

,如果我嘗試打開一個broswer SWF文件中我得到這個錯誤:

SecurityError:[SecurityErrorEvent type ="securityError" bubbles = false cancelable = false evenPhase=2 text="Error#2048"

我怎麼能使.swf文件工作?有沒有辦法將CSV文件中的數據讀入.swf文件?

謝謝大家!

+0

您是否收到錯誤消息?如果您是,請編輯您的問題以包含錯誤的詳細信息。如果您正在編譯AS3(例如,您在FlashPro/AnimateCC中選擇了「ActionScript 3」項目,那麼Organis在其答案中指出,您不能使用File類(它應該會引發編譯器錯誤,除非您選擇AIR模板當你創建你的.fla) – BadFeelingAboutThis

回答

1

這很可能是一個沙盒問題(安全錯誤)。

要知道發生了什麼,當您使用像URLLoader這樣的異步類時,您應該始終監聽錯誤(而不僅僅是COMPLETE)。

除了添加以下監聽到你的完整的聽衆:

documentLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError); 
documentLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError); 

並添加相應的處理方法:

function onIOError(e:IOErrorEvent):void { 
    //file not found, the event (e) will tell you the path it was trying to load and other details 
    trace(e); 
    //alert the user there was a problem 
} 

function onSecurityError(e:SecurityErrorEvent):void { 
    //file was not allowed to load, sandbox issue 
    trace(e); 
    //alert the user there a problem 
} 

如果你確實得到一個安全/沙箱錯誤,我懷疑,確保您使用正確的安全沙箱設置進行發佈。

轉到您的發佈設置file -> publish settings

您會看到標有Local playback security的下拉列表。確保將其設置爲僅訪問網絡,而不是僅訪問本地的默認訪問權限(如果您的CSV來自服務器)。

+0

我只改變了網絡訪問權限,它不再工作我從錯誤2048打開SWF文件 – SpaghettiFunk

+0

好吧,所以我們已經證實你確實得到了一個安全錯誤。一些可能導致錯誤的事情,如果改變網絡訪問沒有任何區別,那麼很可能是跨域問題。確認您的CSV與您的SWF相關的位置(您的SWF是否在本地運行計算機存儲和您的CSV服務器在某處?) – BadFeelingAboutThis

+0

我解決了,感謝您的所有幫助。主要解決方案很簡單:您必須將CSV文件和SWF文件置於可信文件中。您必須使用Adobe Flash Player控制面板小程序打開SWF文件。然後,您必須在Advanced中導航,並添加SWF所在的目錄和CSV文件。這與Windows 10一起工作。您還需要關注CSV路徑中的斜線/反斜線,因爲您可能在Windows中遇到了一些問題。再次感謝你! – SpaghettiFunk

2

包flash.filesystem。*是AIR運行時包。 Flash IDE在測試時模擬AIR運行時,因此它可以工作,但Flash插件和Flash Projector運行時不支持這些類。

選項1:發佈AIR應用程序。

選項2:避免使用AIR類,加載從那裏SWF就是你需要解析SWF URL相同的文件夾中的數據文件:

// Objtain SWF URL. 
var applicationPath:String = loaderInfo.url; 

// Figure/slash or \ backslash is used as folder separator. 
var systemSlash:String = (applicationPath.indexOf("\\") > -1)? "\\": "/"; 

// Remove SWF name from URL. 
var aSplit:Array = applicationPath.split(systemSlash); 
aSplit.pop(); 
aSplit.push(""); 
applicationPath = aSplit.join(systemSlash); 

// Now applicationPath contains full path to the folder where SWF is. 
// Use it wisely!