Thursday, September 19, 2024

Checking if a File Exists

Sometimes it is necessary to determine if a file exists using actionscript, for example when you have a preloader cue, that loads a series of .jpgs one after another, if one of the files doesnt exist then the cue wouldnt traverse any further, you could of course use a timeout in that scenario but why not just check if the file exists before loading it, so that if necessary you can move onto the next item in the cue almost instantly.

It is possible to determine whether a file exists using the success parameter of the onLoad event handler, the problem lies in the fact that the onLoad event handler for movieclips doesnt work, so to check if an .swf file exists, or an image, you will need to use this method…

Here is a little example, that checks to see if a .swf file exists, and if it does, then it loads it into a movieclip.

fileExists=new LoadVars();
fileExists._parent=this;
fileExists.onLoad=function(success)
{
//success is true if the file exists, false if it doesnt
&nbsp&nbsp&nbsp if(success)
&nbsp&nbsp&nbsp {
&nbsp&nbsp&nbsp //the file exists
&nbsp&nbsp&nbsp &nbsp var nm=this._parent.createEmptyMovieClip("swfHolder",1); //so create a movieclip
&nbsp&nbsp&nbsp &nbsp nm.loadMovie("myfile.swf"); //and load our .swf file into it
&nbsp&nbsp }
}
fileExists.load("myfile.swf") //initiate the test

You can do the same with any file type, even if flash doesnt support the loading of that filetype. You may also be wondering if the above process means you have to load a file twice, the answer is NO. If you are running you flash movie in the browser, then when you check to see if the file exists, the onLoad even handler wont fire until the file has fully downloaded into the browsers cache, but of course the Loadvars object wont do anything with an image file, sound file, or swf file. The next time round, when it comes to actually loading the file to actually display it(swf,image,text) or play it(sound) the file will load almost instantly from the browsers cache. If your flash movie is running on the local machine, then you dont have to worry about loading times anyway, because, as always the loading of a file locally is almost instant.

Guy Watson (or FlashGuru as he is also known) has been an active, well
recognized figure in the Flash community for over four years,
supporting the community with tutorials, source files, moderating the
Flashkit forums, and running his own Flash resource Web site,
FlashGuru’s MX 101. Guy was one of two developers who created the ever
popular, award winning zoom interface for Relevare and now runs his
own company, FlashGuru LTD, which builds Flash Games & Applications
for clients such as Comic Relief, Egg and Channel 4.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

Buy new construction florida home ‹ log in. Traditional rewards and protections offered by credit cards are areas where bnpl services typically fall short.