actionscript 3: Check if an external file exists and if so unhide a movieClip

筅森魡賤 提交于 2019-12-11 08:24:57

问题


I'm trying to check if an external file exists and if so change the value of the visibility of a certain movie clip to true. I know how to do it in AS2, but I'm working in AS3.

This is the AS2 code that I used to work with:

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

How to make it work in AS3? Thanks!


回答1:


Class flash.net.URLLoader. From 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");
}

Don't forget to import required classes.




回答2:


A different way to do it in AS3 (the example assumes that you are searching for the file in the user's documents dir, change it accordingly):

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

if (tmp_file.exists) {
    // File exists
} else {
    // File doesn't exist
}



回答3:


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

if (tmp_file.exists) {
    // File exists
} else {
    // File doesn't exist
}

"Keep it simple stupid" - Kiss

Thanks for this code I was struggling with

 .addEventListener(Event.COMPLETE, Myfunction) 

not firing half the time when in an MC or timeline and not at all when in a class or any included code.

I recommend the Above code if you just want to see if a file is there or not. If your using air for android you can use:

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

To store to native Appdata directory.



来源:https://stackoverflow.com/questions/17081749/actionscript-3-check-if-an-external-file-exists-and-if-so-unhide-a-movieclip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!