Copy link to clipboard
Copied
Hey guys, I am trying to load a single image on to the stage via an xml file.
Here is my xml file
<?xml version="1.0" encoding="utf-8"?>
<pictures>
<pic>image1.jpg</pic>
</pictures>
I'm trying to figure out the actionscript 3 code but I don't know how to code it.
If someone can please point me to the code or tutorial, it would be much appreciated.
I did say to put those last lines into the xmlLoaded function, so the whole code would read like this:
Copy link to clipboard
Copied
You would load the xml by doing this:
var req:URLRequest = new URLRequest("xmlfilename.xml");
var ldr:URLLoader = new URLLoader();
ldr.addEventListener(Event.COMPLETE,xmlLoaded);
ldr.load(req);
function xmlLoaded(e:Event){
var xml:XML = XML(e.target.data);
//see * below..
}
* then the code to load the image could go in the xmlLoaded function (in place of that //see below line), and would look like this:
var imgreq:URLRequest = new URLRequest(xml.pictures.pic[0]);
var imgldr:Loader = new Loader();
imgldr.load(imgreq);
addChild(imgldr);
Normally you might add a listener to the Loader's contentLoaderInfo, so that you could then size or position the image before adding it to the stage. But hopefully the above code would get you as far as having the image in the top left of the stage.
Read up more on Loader, URLRequest, URLLoader, and XML, to get a sense of the various parts of the whole process.
Copy link to clipboard
Copied
Hey Colin thanks for the code. It's still giving an error of "Access of undefined property pictures"
Error points to this line of code - var imgreq:URLRequest = new URLRequest(xml.pictures.pic[0]);
Here is the whole code
var req:URLRequest = new URLRequest("logo.xml");
var ldr:URLLoader = new URLLoader();
ldr.addEventListener(Event.COMPLETE,xmlLoaded);
ldr.load(req);
function xmlLoaded(e:Event){
var xml:XML = XML(e.target.data);
}
var imgreq:URLRequest = new URLRequest(xml.pictures.pic[0]);
var imgldr:Loader = new Loader();
imgldr.load(imgreq);
addChild(imgldr);
Copy link to clipboard
Copied
I did say to put those last lines into the xmlLoaded function, so the whole code would read like this:
Copy link to clipboard
Copied
Hey Colin,
Again, thanks for the code. The only thing I had to change was this line
var imgreq:URLRequest = new URLRequest(xml.pictures.pic[0]); to
var imgreq:URLRequest = new URLRequest(xml.pic[0]);
Just took the picture and got it to work.
Just for anyone who might be interested the whole code is...
Copy link to clipboard
Copied
Just for interest, if you had done this (which is what my eyes saw):
<xml>
<pictures>
<picture>apic</picture>
</pictures>
</xml>
then you would have needed to do it the xml.pictures.picture way. Because you did this instead:
<xml namespacestuff />
and no </xml> at the end, you got away with leaving out the pictures level.
Copy link to clipboard
Copied
Thanks for the insight. I will keep that in mind for next time