Groups | Blog | Home
all groups > flash actionscript > january 2007 >

flash actionscript : XML fail global listener


Mr Helpy mcHelpson
1/10/2007 11:42:17 PM
So I have a swf file that pulls other swfs, and eventually these swfs pull XML
whenever they want. What I'd like is something to tell me if ANY the XML load
fails on the main movie, so I can trigger an event. I know I can go into each
XML loading function and code in an "on failure" method, but I'd enjoy just 1
listener doing all the work. Is it possible?

does that make sense?
abeall
1/11/2007 3:43:04 AM
Ah, you could try using a prototype:

XML.prototype.onLoad = function(success){
if(!success) trace(this+" has failed to load!");
}

But then it would be impossible(or very annoying and ugly) to make any custom
onLoad for any XML thereafter.

Alternatively, XML.loaded might be of some help.
LuigiL
1/11/2007 1:52:43 PM
That would require a custom class that extends the XML class. That way you can
override the onLoad event and handle the exceptions. Attached a class I use,
customised to fit your needs.
The constructor of the class demonstrates how the catch an error with the file
extension. Delete it if you want.
You pass a path and a reference to the movieclip to the constructor. The
onLoad method instructs your movieclip to play (you can change that to any
action you want) when loading is successful or handles exceptions based on the
(instance) name of the clip. Again, you can change the actions there to suit
your needs. (yes, if else switch works...)



/**
* CustomXML, Version 1
* Custom class to load a xml file.
* Overrides methods of the XML Class to
* handle exceptions when loading fails.
*
* Dependencies: XML.as
*
* @author: Peter Lorent, 2006/2007
* luigil@mac.com for comments on this class
* @version 1.0.0
*
* Usage:
* in the movieclip:
* var my_xml:CustomXML=new CustomXML("path",this);
*
*/

class CustomXML extends XML {

//class definition
//private properties
private var thePath:String;
private var _clip:MovieClip;

//class body
//constructor

/**
* Initializes the new instance.
* Catches an error with the file extension and handles it
* by trying to load the file with the extension .xml
* (alternatively just throw the error)
*
* @param p String. The path to the xml file.
* @param clip MovieClip. Reference to the calling movieclip.
*
*/
public function CustomXML(p:String,clip:MovieClip){
super(p);
ignoreWhite=true;
try{
var isPath:Boolean=(p.slice(-4)==".xml");
if(!isPath){
this.firstChild.removeNode();
throw new Error("Not a valid file extension!");
} else {
thePath=p;
_clip=clip;
this.load();
}
} catch(e:Error){
//handle this exception
trace("An error occured: "+e.message+newline+"Trying to load with extension
..xml...");
//try loading the file with extension .xml
var file:Array=p.split(".");
var newPath:String=file[0]+".xml";
thePath=newPath;
_clip=clip;
this.load();
//or pass the error to the calling method
//throw e;
}
}

/**
* Calls load() method in the super class.
*
*/
private function load():Void{
super.load(thePath);
}

/**
* Overrides the onLoad() method of the super class.
* When loading fails you can throw error messages based
* on the instance name of the provided movieclip
* This example assumes you have a text field on the timeline
* of the clip to display the error message.
* (change to suit your needs)
*
* @param s Boolean.
*
*/
private function onLoad(s:Boolean):Void{
try{
if(s){
_clip.play();
} else switch(_clip._name){//check based on instance name of the movieclip
case "box_mc":
throw new Error("Unable to load the data for this section. Please press
'Proceed'.");
break;
case "box2_mc":
throw new Error("Unable to load the data. Redirecting in 5 seconds...");
break;
}
} catch(e:Error){
//handle the exception
//assumes a errorTxt text field in the clip
_clip.errorTxt.text=e.message;
//and any other action...
}
}

}
Mr Helpy mcHelpson
1/12/2007 4:33:56 PM
LuigiL
1/12/2007 4:51:46 PM
AddThis Social Bookmark Button