ActionScript 3.0 .fla 文件工作,.swf 文件导出不工作

ActionScript 3.0 .fla file working, .swf file exported not working

我还有一个关于 acrionscript swf 文件的问题。我正在从 CSV 文件中读取一些文本。我创建了 fla 文件,当我在 animate 编辑器中测试它时它可以工作,我可以看到 CSV 文件中的所有文本都变成了 TextField。如果我使用 Adob​​e Flash Player 11.4 r402 导出 .swf 和 运行,我看不到 CSV 文件中的文本。我创建了这个由一张照片组成的 fla 文件,我在其中写了这段代码:

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);
}

如果我尝试在浏览器中打开 swf 文件,我会收到此错误消息:

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

如何使 .swf 文件工作?有没有办法将数据从 CSV 文件读取到 .swf 文件?

谢谢大家!

这很可能是沙盒问题(安全错误)。

要了解发生了什么,当您使用异步 类 时,例如 URLLoader.

,您应该始终监听错误(而不仅仅是完成)

除了完整的侦听器之外,还添加以下侦听器:

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
}

如果我怀疑您确实遇到了 security/sandbox 错误,请确保您使用正确的安全沙箱设置进行发布。

转到您的发布设置 file -> publish settings

您会看到一个标记为 Local playback security 的下拉列表。确保将其设置为仅访问网络,而不是默认的仅本地访问(如果您的 CSV 来自服务器)。

Package flash.filesystem.* 是 AIR 运行时包。 Flash IDE 在测试时模拟 AIR 运行时,因此它可以工作,但 Flash 插件和 Flash 投影仪运行时不支持这些 类。

选项 1:发布 AIR 应用程序。

选项 2:避免使用 AIR 类,从您需要解析 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!