View Full Version : If image not loaded, load another image
vpsedillo
12-23-2004, 08:25 AM
How can i load another image if the image i loaded wasnt found? I want to load a default image for every non-existing images :)
Thank you in advance :D
Mortimer Jazz
12-23-2004, 05:18 PM
To keep it simple, one approach that should suffice for the vast majority of situations is to use an incrementing variable as a counter.
If, by the time your counter has reached a limit (that's been specified by you) the getBytesLoaded of the target clip is still less than 0* it would be safe to assume the picture may not exist, and you can load your default image instead.
//this code is not tested but should give you the general idea...
var count:Number = 0;
var max:Number = 100; //low - up to you to set a reasonable delay
var arb:Number = 10; //see note below
someMC.loadMovie("doesnotexist.jpg");
this.onEnterFrame = function()
{
trace(count++);
gbl = someMC.getBytesLoaded();
gbt = someMC.getBytesTotal();
if(gbl == gbt && gbl > arb){
trace("loaded");
this.onEnterFrame = null;
}
if(gbl < arb && count >= max)
{
trace("load default picture");
count = 0;
this.onEnterFrame = null;
}
}
As I mentioned, there are various ways and it depends how accurate you want to be and how you're loading your pics (for example, if you use the MovieClipLoader API you may want to use MovieClipLoader (http://www.nwebb.co.uk/nw_htmlsite/index.php?page=browse_tutorial&tutorial=mcl1&part=1).onLoadError() ). The way I've shown here is perhaps the easiest, but certainly not the most fail safe.
*One caveat to be aware of is that if you place a MovieClip on stage at authortime and trace its getBytesLoaded() you should see that it traces aprox 4 bytes initially (it will continue to do so until the loadMovie method is called - then it will eventually end up tracing 0 bytes if the pic can't be found). However if you create a MovieClip at run time instead, then getBytesLoaded should start off as 0. To allow for both situations I've used an arbitrary value of 10 in the above code.
Sorry for the rushed response (must go on my way home for xmas now!)
flyingchalupa
12-23-2004, 06:39 PM
Flash Guru posted a way of checking if a file exists... I am sure you can make this work for what you need...
http://www.flashguru.co.uk/000310.php
Cheers...
vpsedillo
12-24-2004, 03:53 AM
hmm....il test those codes after christmas.. im on vacation, my codes at home :) thank you..
Merry Christmas to you both and a happy new year...
:D
Mortimer Jazz
12-24-2004, 08:47 AM
That's a neat solution except (unless I've missed something) onLoad doesn't fire until the entire file has loaded, so in order to give any loading-progress feedback you need to track the bytesLoaded and bytesTotal of the LoadVars object, which seems to negate most of the benefits (?)
vpsedillo
12-28-2004, 08:37 AM
I have tried Mortimer Jazz's code and it works, but that of Flashguru's file exist tutorial code suits better since my projector will be running in a CD and not via the internet.
The purpose of this actually is that when a user clicks an image in a gallery, it then loads two images, the actual and the detail view, if ever there's no detail view then it wont load anything, thats why i want it to load a default image instead. :D
fileExists=new LoadVars();
fileExists.onLoad=function(success)
{
if(success){
detail.loadMovie("images/images/" + passx + "-DE.jpg");
}
else {
detail.loadMovie("images/images/default.jpg");
}
}
fileExists.load("images/images/" + passx + "-DE.jpg");
Thank you Mortimer Jazz and flyingchalupa for the response. Advance Happy New Year. :D
vBulletin® v3.7.1, Copyright ©2000-2009, Jelsoft Enterprises Ltd.