• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Loading Single Image Via XML

Guest
Apr 30, 2009 Apr 30, 2009

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.

TOPICS
ActionScript

Views

691

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Apr 30, 2009 Apr 30, 2009

I did say to put those last lines into the xmlLoaded function, so the whole code would read like 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);
  var imgreq:URLRequest = new URLRequest(xml.pictures.pic[0]);
  var imgldr:Loader = new Loader();
  imgldr.load(imgreq);
  addChild(imgldr);
}
This is email code by the way, but might we
...

Votes

Translate

Translate
LEGEND ,
Apr 30, 2009 Apr 30, 2009

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 30, 2009 Apr 30, 2009

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);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 30, 2009 Apr 30, 2009

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:

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);
  var imgreq:URLRequest = new URLRequest(xml.pictures.pic[0]);
  var imgldr:Loader = new Loader();
  imgldr.load(imgreq);
  addChild(imgldr);
}
This is email code by the way, but might well still work! I don't want to have all of the fun, you should have some too, in fixing anything I may have mistyped.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 01, 2009 May 01, 2009

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...

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);
  var imgreq:URLRequest = new URLRequest(xml.pic[0]);
  var imgldr:Loader = new Loader();
  imgldr.load(imgreq);
  addChild(imgldr);
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 01, 2009 May 01, 2009

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 01, 2009 May 01, 2009

Copy link to clipboard

Copied

LATEST

Thanks for the insight. I will keep that in mind for next time

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines