1

我正在尝试检查是否存在外部文件,如果存在,则将某个影片剪辑的可见性值更改为 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 中工作?谢谢!

4

3 回答 3

4

在 AS3 中执行此操作的另一种方法(示例假设您正在用户的文档目录中搜索文件,请相应地进行更改):

var tmp_file:File = File.documentsDirectory.resolvePath('my_file.txt');

if (tmp_file.exists) {
    // File exists
} else {
    // File doesn't exist
}
于 2014-10-09T15:50:44.880 回答
4

flash.net.URLLoader。来自Adob​​e ActionScript 3.0 参考

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

不要忘记导入所需的类。

于 2013-06-13T08:09:00.523 回答
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 或时间线中不会触发一半的时间,而在课堂或任何包含的代码中则根本不会触发。

如果您只想查看文件是否存在,我推荐上面的代码。如果您使用 air for android,您可以使用:

File.applicationStorageDirectory.resolvePath("my_file.txt");

存储到本机 Appdata 目录。

于 2016-03-16T02:10:25.443 回答