PDA

View Full Version : How can I substitute xml file with another in AS3



elindividuo123
September 7th, 2009, 11:37 PM
Hi, I'm fairly new at AS3 and I'm having a though time trying to replace an xml with another. I have five buttons that link to different picture galleries. All of them work with different xml files. The first xml file is called from an external as file. (CenteredSlideShow.as). I can load five different xml files by using modified versions of this code but it ends up being a whole lot of code on my .fla. and the other pictures remain loaded.



var xmlPath = "ilustrationsAS3.xml";
var imagePath="CrouchingCity.jpg";

function onXMLLoad(event:Event):void {
var xml:XML = new XML(event.target.data);
xmlCatch=xml;
xmlLength = (xml.pictures.picture.length());
currentWidth = (xml.pictures.picWidth[currentIndex]);
imagePath = (xml.pictures.picture[currentIndex]);
currentHeight = (xml.pictures.picHeight[currentIndex]);
parse();
}
var loader:URLLoader = new URLLoader();
var url:URLRequest = new URLRequest(xmlPath);
loader.addEventListener(Event.COMPLETE, onXMLLoad);
loader.load(url);

function parse () :void {
var photos:URLRequest = new URLRequest (imagePath);
photoLoader.load(photos);
picAlign();
addChild(photoLoader);
}


I had figured this out for AS2 but loading xml files in AS3 is completely different. Is there any way to make this code reusable, so as to not have it five times along with about a zillion vars? I would really apreciate any help.

thatsasif
September 8th, 2009, 02:31 AM
Simple Write this code in a class file

elindividuo123
September 8th, 2009, 10:14 PM
Thanks thatsasif. I hadn't thought about that. I'm definitely gonna do that although I think I need to make it more generic in order for it to work as a class and I'm sorry if this is an obvious question but...

I'm not - as you can probably tell- a professional programer. I'm sort of learning as I go.

Now, I'm sure I can make it a class and reuse it to load all my xml files, but I need to figure out how to remove a loaded xml. I have no code for it. I don't know how this is done.

So far I've tried making the images invisible using "photoLoader.visible=false;"

The problem is that even though the user cannot see the images, they remain loaded and they make my web page rather slow.

I also tried adding this code to my as file:


function chooseLoaded (curUrl):void {
currentIndex=0;
loader.load(curUrl);
}

Then for each button adding this:


function cineClick(event:MouseEvent):void {
xmlPath= "photos.xml";
var url2:URLRequest =new URLRequest(xmlPath);
curUrl =url2;
chooseLoaded (curUrl);
}

function hacerClick(event:MouseEvent):void {
xmlPath= "ilustrationAS3.xml";
var url2:URLRequest =new URLRequest(xmlPath);
curUrl =url2;
chooseLoaded (curUrl);
}

It seems to work once for every button but if you press any button a second time it doesn't do anything. My guess is that the xml remains loaded even though it disappears, but really I don't know.

Again, thank you so very much.