EMB01
asked on
Displaying XML Results in Multiple Objects
How do I use ActionScript to program the results of an XML file to be displayed in three different movieclips. For example, the XML file would have three entries (images) and there are three movieclips in the animation - each of the movieclips would display one of the three images from the XML file.
ASKER
Thank you; but is there anyway to do it so the images are not at random positions?
ASKER
Optimally, I want them (the results of the XML document) to go into three specified movieclips (i.e. mc1, mc2, mc3, etc.).
yes it is very simple actually. The sample I sent you is more complex than what you want.
Put 3 movieclips on stage as you want, let's say you name them mc1,mc2,mc3.
//Actionscript 3.0
var xmlImages:XML = <images>
<image>img/image1.jpg</ima ge>
<image>img/image2.jpg</ima ge>
<image>img/image3.jpg</ima ge>
</images>;
var mcs:Array = [mc1,mc2,mc3];
var i:int = 0;
for each(mc:MovieClip in mcs){//iterates through each mc in the array
var imgldr:Loader = new Loader();//this is kind of movieclip, but it can load external content
imgldr.load(new URLRequest(xmlImages.image [i])); //You should have number of images >= number of mcs
mc.addChild(imgldr);//adds the loader to each mc, so it can be seen
}
Put this code in the first frame of Actionscript 3.0 document, and put the 3 images in the xml in the same folder. This will load the 3 images and put them into 3 pre-defined movieclips
Put 3 movieclips on stage as you want, let's say you name them mc1,mc2,mc3.
//Actionscript 3.0
var xmlImages:XML = <images>
<image>img/image1.jpg</ima
<image>img/image2.jpg</ima
<image>img/image3.jpg</ima
</images>;
var mcs:Array = [mc1,mc2,mc3];
var i:int = 0;
for each(mc:MovieClip in mcs){//iterates through each mc in the array
var imgldr:Loader = new Loader();//this is kind of movieclip, but it can load external content
imgldr.load(new URLRequest(xmlImages.image
mc.addChild(imgldr);//adds
}
Put this code in the first frame of Actionscript 3.0 document, and put the 3 images in the xml in the same folder. This will load the 3 images and put them into 3 pre-defined movieclips
UPDATE:the code has an error, here it is again:
var i:int = 0;
for each(mc:MovieClip in mcs){//iterates through each mc in the array
var imgldr:Loader = new Loader();//this is kind of movieclip, but it can load external content
imgldr.load(new URLRequest(xmlImages.image [i])); //You should have number of images >= number of mcs
mc.addChild(imgldr);//adds the loader to each mc, so it can be seen
i++;
}
var i:int = 0;
for each(mc:MovieClip in mcs){//iterates through each mc in the array
var imgldr:Loader = new Loader();//this is kind of movieclip, but it can load external content
imgldr.load(new URLRequest(xmlImages.image
mc.addChild(imgldr);//adds
i++;
}
ASKER
I'll give that a shot. Just to make sure, I am using an external XML document; so does it matter when the XML structure is as has been attached?
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<images>
<pic>
<image>http://www.kirupa.com/developer/mx2004/pg/kresge.jpg</image>
<caption>Kresge</caption>
<url></url>
</pic>
<pic>
<image>http://www.kirupa.com/developer/mx2004/pg/medialab.jpg</image>
<caption>Media Lab</caption>
<url>http://www.emarketbuilders.com/</url>
</pic>
<pic>
<image>http://www.kirupa.com/developer/mx2004/pg/stata.jpg</image>
<caption>Stata Center</caption>
</pic>
<pic>
<image>http://www.kirupa.com/developer/mx2004/pg/stata_lobby.jpg</image>
<caption>Stata Lobby</caption>
</pic>
<pic>
<image>http://www.kirupa.com/developer/mx2004/pg/construction.jpg</image>
<caption>Construction</caption>
</pic>
<pic>
<image>http://www.kirupa.com/developer/mx2004/pg/dome.jpg</image>
<caption>The Dome</caption>
</pic>
<pic>
<image>http://www.kirupa.com/developer/mx2004/pg/structure.jpg</image>
<caption>Structure</caption>
</pic>
</images>
Well, you said tou wanted to know how to parse xml and use it's info to do load images in movieclips, but based on the xml, I can see you are trying to make somthing like a menu?
and the structure of the xml matters. it differs the way you reach nodes depending on the xml tree
ASKER
Exactly right, what I have now is basically a slideshow with links; but I want each of the entries to instead show up side by side. How much would the code have to change to make this possible?
some of it. You want it to be static? no interaction? I'll make a sample displaying each image in the xml side by side statically.
ASKER
When you say "no interaction" do you mean without links? In that case, I would prefer for there to be links.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This is probably a stupid question... How do I save the code snippet as an .as file? Also, is there anyway to accomplish this without having to use classes and AS3? Could we do it in AS2, perhaps; and still use classes? Sorry for the trouble.
open flash , create new actionscript file, copy the code snippet and paste there. Yes it can be done without classes and in as2. Yes it can be done in as3 without classes too. But your question was:
"How do I use ActionScript to program the results of an XML file to be displayed in three different movieclips"
As you did not specify that you wanted in as 2, and also accepted the first code I sent as 3 I did it in as 3. Also you asked how to program results of xml, so I assumed you knew how to acquire the xml by coding. In AS 2 the handling of XML is totally different, cause in as3 XML is treated as E4X spec.
"How do I use ActionScript to program the results of an XML file to be displayed in three different movieclips"
As you did not specify that you wanted in as 2, and also accepted the first code I sent as 3 I did it in as 3. Also you asked how to program results of xml, so I assumed you knew how to acquire the xml by coding. In AS 2 the handling of XML is totally different, cause in as3 XML is treated as E4X spec.
ASKER
Sorry, julianopolito - I have not been clear. I have Flash MX 2004; I didn't realize the different versions would be so... different. If you please, I can open a new question that is specific to ActionScript 2.0 and I can accept this as a solution for ActionScript 3.0?
ok, there is no problem. I'll then do an AS 2 version for you. I'm sorry for the inconvenience.
ASKER
I have posted the new AS2-specific question here: https://www.experts-exchange.com/questions/23141218/Displaying-XML-Results-in-Multiple-Objects-w-AS2.html.
var xmlImages:XML = <images>
<image>img/image1.jpg</ima
<image>img/image2.jpg</ima
<image>img/image3.jpg</ima
</images>;
for each(image:XML in xmlImages.image){//xmlImag
var imgldr:Loader = new Loader();//this is kind of movieclip, but it can load external content
imgldr.load(new URLRequest(image.toString(
imgldr.x = Math.random()*300;//positi
imgldr.y = Math.random()*300;
addChild(imgldr);//adds the loader to stage, so it can be seen
}
Put this code in the first frame of Actionscript 3.0 document, and put the 3 images in the xml in the same folder. This will load the 3 images and put them at random points on stage.