Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If file exists, load it, else insert library item

Status
Not open for further replies.

Leozack

MIS
Oct 25, 2002
867
GB
I've googled around for a while tonight for things like "actionscript 1 check file exists" and so on, and the best I find is people saying to check if a file exists, do this
Code:
fileExists=new LoadVars();
fileExits._parent=this;
fileExists.onLoad=function(success)
{
//success is true if the file exists, false if it doesnt
if(success)
{
//the file exists
var nm=this._parent.createEmptyMovieClip("swfHolder",1); //so create a movieclip
nm.loadMovie("myfile.swf"); //and load our .swf file into it
}
}
fileExists.load("myfile.swf") //initiate the test
The problem is while that may itself work, I can't put all that in for every image load, and I can't make it into any kind of function because the function will always return false when called because the image doesn't load until later. And I can't pass the function what to do when it loads etc. So yeah I've tried all sorts. The code I'm really trying to come up with would make this possible
Code:
If (DoesFileExist(filename)) { ShowImage(clip,filename); }
Else { clip.attachMovieClip("libitem","libitem",1); }
or maybe
Code:
var didItWork = ShowImage(clip,filename);
if (!didItWork) { clip.attachMovieClip("libitem","libitem",1); }
That way I can simply load images if they're there and if now I can load a library item that is a 'image not found' symbol. But frankly I can't for the life of me do it -_-

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
Someone else suggested something like this
Code:
function checkFileExists(i_file:String, i_functionName:String)
{
    fileExists = new LoadVars();
    thisVal = this;
    fileExists.onLoad = function(success)
    {
        //success is true if the file exists, false if it doesnt
        if (success)
        {
            //the file exists
            //call back
            thisVal[i_functionName](i_file);
        }
    };
    fileExists.load(i_file);
}
//
function doSomethingWithImageItExists(i_file:String)
{
    trace("lets load now it exists!  " + i_file);
}
//
checkFileExists("test.swf", "doSomethingWithImageItExists");

But I've not yet got it to work :/ Great to know the feature is finally in CS3 but when such a basic feature isn't available in earlier ones people had to get by somehow didn't they? Did people just not check if a file exists before trying to use it or whatever? Hmmm.

I don't want ot use php's fileexists as that would be an extra requirement of php on a server purely for the purpose of that 1 function :(

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
Ok so after removing the strong typing typecasts on the above functions, it worked o_O Maybe AS1 doesn't like them or something but once I removed those it worked.
However - whilst I can now fire off a function if the imagefile exists and loads successfully - I can't find a way to load an alternate error message image or perform some other code if it doesn't. And I can't find a way to load the error image into a clip and then use this stuff to check if a real image exists and if so laod it into the clip instead of the error image, acheiving the same result as loading images if they exist and getting an error image if they don't. I'm trying various code here but gonna have to stop for the night :(

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
There is no type casting in AS1.

This is how I'd do it:
Code:
function checkFileExists(i_file, successFunction, fallBackFunction) {
	fileExists = new LoadVars();
	fileExists.onData = function(src) {
		if (src != undefined) {
			successFunction(i_file);
		} else {
			fallBackFunction(i_file);
		}
	};
	fileExists.load(i_file);
}
// success function
function doSomethingWithImageItExists(i_file) {
	trace("lets load now it exists!  "+i_file);
}
// fallback function
function doSomethingIfNot(i_file) {
	trace(i_file+" does not exist :(");
}
//
checkFileExists("test.swf",doSomethingWithImageItExists,doSomethingIfNot);


Kenneth Kawamoto
 
It didn't like your direct insersion of variables for function names - using the previous syntax of myVal = this and myVal[functionname](args) worked ok though. So I've rounded it off with the ability to pass arguments to those functions in an array, eg

Code:
// Checking Function
function checkFileExists(i_file, successFunction, failFunction, functionArgs) {
    fileExists = new LoadVars();
	thisVal = this;
    fileExists.onData = function(src) {
        if (src != undefined) {
            thisVal[successFunction](functionArgs);
        } else {
            thisVal[failFunction](functionArgs);
        }
    };
    fileExists.load(i_file);
}
// Success Function
function fileFound(argumentArray) {
	for (i=0; i < argumentArray.length; i++) {
		trace("Success!  Arguments passed : " + i + " - " + argumentArray[i]);
	}
}
// Fail Function
function fileNotFound(argumentArray) {
	for (i=0; i < argumentArray.length; i++) {
		trace("Failiure!  Arguments passed : " + i + " - " + argumentArray[i]);
	}
}
// Code Body
testArr = new Array(imgHolderMC,imgWidth,imgHeight,imgFilename,imgMaskMC);  // Just a bunch of variables
checkFileExists(thisFile, "fileFound", "fileNotFound", testArr);

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
I didn't say I didn't like the direct variables as function names, I said IT didn't, aka flash :p It didn't work untilI changed them to the thisvariable[functionname] style but there ya go :)

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
Maybe it's a flash6mx thing then. You live and learn!

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top