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!
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.
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
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.