我正在尝试检查是否存在外部文件,如果存在,则将某个影片剪辑的可见性值更改为true。我知道如何在AS2中这样做,但我在AS3工作。
这是我以前使用的AS2代码:
onClipEvent (load) {
fileExists = new LoadVars();
fileExists._parent = this;
fileExists.onLoad = function(success) {
//success is true if the file exists, false if it doesnt
if (success) {
_root.visiblity = 1;
//the file exists
}
};
fileExists.load('visibility.exe');//initiate the test}
}
如何让它在AS3中运行?谢谢!
答案 0 :(得分:4)
班级flash.net.URLLoader
。来自Adobe ActionScript 3.0 Reference:
var urlRequest:URLRequest = new URLRequest("visibility.exe");
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, urlLoader_error);
urlLoader.load(urlRequest);
function urlLoader_complete(evt:Event):void {
trace("file found");
}
function urlLoader_error(evt:IOErrorEvent):void {
trace("file obviously not found");
}
不要忘记导入所需的课程。
答案 1 :(得分:4)
在AS3中执行此操作的另一种方法(该示例假定您在用户的文档目录中搜索该文件,相应地更改它):
var tmp_file:File = File.documentsDirectory.resolvePath('my_file.txt');
if (tmp_file.exists) {
// File exists
} else {
// File doesn't exist
}
答案 2 :(得分:0)
var tmp_file:File = File.documentsDirectory.resolvePath('my_file.txt');
if (tmp_file.exists) {
// File exists
} else {
// File doesn't exist
}
"保持简单愚蠢" - 吻
感谢您使用此代码我正在努力
.addEventListener(Event.COMPLETE, Myfunction)
在MC或时间轴中没有发射一半的时间,在课堂或任何包含的代码中没有发射。
如果您只想查看文件是否存在,我建议使用Above代码。 如果你使用air for android你可以使用:
File.applicationStorageDirectory.resolvePath("my_file.txt");
存储到本机Appdata目录。