View Full Version : Fairly simple XML/Flash question
pappageo
May 1st, 2007, 10:11 PM
Hi, I've searched alot for this but can't seem to find a quick simple answer to my problem. Everytime I search all I seem to find is stuff that is way to complex for what I am looking for!
All I am trying to do is load a jpg into a movieclip in flash using an external xml file.
This is what I am using as my .xml file
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<images>
<photo>
<image>myimage.jpg</image>
</photo>
</images>
Is there a simple way to load "myimage.jpg" into a blank movieclip with the instance name "mymovieclip"? I have a feeling it is fairly simple I just can't seem to find a tutorial or thread discusing it in this simple of a manner.
Thanks so much to anyone who is willing to shoot me some quick help!
TNE
May 2nd, 2007, 02:47 AM
Hi, what you need to do is to either create an emptyMovieClip, through actionscript, with the name mymovieclip, or have one on stage with the same instance name.
Then , load the XML into and XML object, access the relative url that you have provided to the image [in this case it is "myimage.jpg"], and load that image into the empty movie clip.
Here is sample code.
Please note, that you need an XML file called image.xml and an image called myimage.jpg in the same path as the SWF. The contents of the XML file are exactly the same as what you've mentioned.
_root.createEmptyMovieClip("mymovieclip",this.getNextHighestDepth());
mymovieclip._x = 0;
mymovieclip._y = 0;
var imageURL;
var XMLToLoad:XML = new XML();
XMLToLoad.ignoreWhite = true;
XMLToLoad.onLoad = loadImage;
function loadImage(success:Boolean):Void{
if(success)
{
imageURL = this.firstChild.childNodes[0].childNodes[0].firstChild.toString();
trace(this.firstChild)
mymovieclip.loadMovie(imageURL,this.getNextHighest Depth());
}
else
{
//Loading failed
}
}
XMLToLoad.load("image.xml");
~TNE
pappageo
May 2nd, 2007, 04:05 AM
Perfect! Thank you so much. You helped me out alot.
Now, if I may ask uno mas. What if I were to have more photos listed in the xml file, like this...
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<images>
<photo>
<image>slides/myimage1.jpg</image>
</photo >
< photo >
<image>slides/myimage2.jpg</image>
</photo >
</images>
How would I go about loading the second image in the list, "myimage2.jpg" into another movieclip on the stage? I have a feeling that might be just a slight change to the code you listed above... I'm doing some more research on xml right now and trying to figure it out myself but any help you can give is greatly appreciated!!!
Nick
TNE
May 2nd, 2007, 05:26 AM
No Problem.
As to your second question, Do you intend to have a predefined number of images in the XML file ? Or can that number vary (dependant on certain factors or whtever...)
I ask because the two situations differ in the terms of complexity the code has to possess.
The basic idea is that the line
this.firstChild.childNodes[0].childNodes[0].firstChild.toString()
which refers to one node, can be made to refer to a node in a sequence of nodes when put in a for loop.
In your case, according to the structure you have defined, this modification would be:
this.firstChild.childNodes[i].childNodes[0].firstChild.toString()
where "i" is the variable in the loop which shall iterate according to the number of nodes of your XML file.
~TNE
pappageo
May 2nd, 2007, 05:50 AM
What I am ultimately trying to do is load 6 different jpgs into 6 separate movieclips in flash for a sort of self revolving slideshow where each slide has a its own button.
I actually got it working the way I want it too (pretty much by complete accident) but you can see what I am going for here: http://www.nickpappageorgas.net/slideshow/tester2.html
The way I did it was to add the actionscript below into 6 consecutive frames on my actions layer and changing the number you told and the movieclip instance name each time.
var imageURL;
var XMLToLoad:XML = new XML();
XMLToLoad.ignoreWhite = true;
XMLToLoad.onLoad = loadImage;
function loadImage(success:Boolean):Void{
if(success)
{
imageURL = this.firstChild.childNodes[1].childNodes[0].firstChild.toString();
trace(this.nextSibling)
slide02.loadMovie(imageURL,this.getNextHighestDept h());
}
else
{
//Loading failed
}
}
XMLToLoad.load("image.xml");
Is there a cleaner way of loading these 6 images into 6 different movieclips that you can suggest? As I said though, I DO have it working right now so if its too much effort for you to explain I'm alright with it being messy the way it is.
Also I'm sure there is a WAY better way of doing this using xml and actionscript but as you can see I'm not terribly experienced in either so I figured I would just worry about getting the images into movieclips where I better knew how to work with them.
Thank you again for your help! It took A LOT of stress off of my night!
TNE
May 3rd, 2007, 05:48 AM
Glad to help.
Yes you are correct, you could employ another method to make it much cleaner instead of having to place that code in each frame.
I'll explain the code path here:
Find out the number of Images in the XML, and then enter a for loop for that number.
In each iteration, you would create an empty movie clip, set its x and y coordinates (usually 0,0) and then load the corresponding image into that clip.
So if you have six images, you would get six clips created each with a image loaded inside of it... and they would be placed on top of each other so the last one that loaded would be visible.
Then in the first frame, you would have to set the appriate clip as the visible one and the rest would be invisible.
After that, the action on each of the buttons would make their corresponding clip visible and the rest invisible.
Of course, please note that since you are loading all the images in the first frame it self , there will be a slight loss in the speed of loading the swf initially for a very high number of high quality images.
~TNE
mxsailor
October 2nd, 2007, 02:12 AM
Eavesdropping on this thread... is it possible to change the coordinates for a MC using XML? Have a MC appearing at different locations on the stage using an actionscript timer?
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.