View Full Version : Very basic XML text and image
pete@mymettle.com
03-04-2009, 05:50 AM
I would like to create a SWF that dynamically loads one image into a movie clip and HTML editable copy into a large text box. Nothing fancy at all. no need for a slideshow or anything like that. Just one image and some text. I know this should be easy for me to figure out. Could someone please help me out.
Thanks,
Wilson
What have you tried so far?
endergrl
03-04-2009, 03:57 PM
you need something like this to set up your xml
import mx.xpath.XPathAPI;
var myXML:XML = new XML();//creates xml
myXML.ignoreWhite = true;//parameter for xml
myXML.load(formatPath);//formatPath is the path to the xml file
myXML.onLoad = function(success) {
setupStage();
};
notice the import mx.xpath.XPathAPI;. I use this api instead of flash's built in xml function.
function setupStage(){
var imagePath:String = XPathAPI.selectSingleNode(myXML.firstChild, "/content/background").firstChild.nodeValue;//the content/background is the path to the node that defines the image path
createEmptyMovieClip('imageHolder_mc',50);
imageHolder_mc.loadMovie(bgPath,imageHolder_mc);
imageHolder_mc._y = 0;
imageHolder_mc._x = 0;
var textfield:TextField = help_mc.createTextField("textfield", 1, 100, 10, 150, 20);
imageHolder_mc.textfield.multiline = true;
imageHolder_mc.textfield.embedFonts = true;
imageHolder_mc.textfield.wordWrap = true;
imageHolder_mc.textfield.autoSize = true;
imageHolder_mc.textfield.selectable = false;
imageHolder_mc.textfield.html = true;
var my_tf:TextFormat = new TextFormat();
my_tf.font = 'Arial';
my_tf.align = 'left';
my_tf.color = 0x000000;
my_tf.size = 18;
my_tf.bold = true;
imageHolder_mc.textfield.htmlText = XPathAPI.selectSingleNode(myXML.firstChild, "/content/myText").firstChild.nodeValue;
imageHolder_mc.textfield.setTextFormat(my_tf);
};
}
you would need to embed arial or whatever font family that you are using. you could also create the movie clip and text filed onstage in which case you can take out the creation parts of my code and just leave the code where you set things = to the xml value.
here's what the xml should look like
<content>
<background>imagepath here</background>
<myText> your text here</myText>
</content>
pete@mymettle.com
03-04-2009, 04:02 PM
I have gotten images to load fine but I cant figure out the text. Of course I would like to use the same xml file for both.
Here is what I have that works for the image:
In AS
myXML = new XML();
myXML.ignoreWhite = true;
myXML.onLoad = function(success){
if(success){
// the line below load the pictures in an empty movie clip manually created and called imageLoader
loadMovie(myXML.childNodes[0].firstChild.firstChild.nodeValue, imageLoader);
} else {
}
}
myXML.load("myImage.xml");
in XML
<myImage>
<imagePath>01.jpg</imagePath>
</myImage>
Whenever I tried to add a node with text and retrieve it with Actionscript it doesnt work. I am using AS2 in Flash 8.
endergrl
03-04-2009, 04:40 PM
where are you trying to add the text node? if you add it before the image path node it will break your code because you define the path as node 0. following the same code format you would just change the 0 to a 1 and set your textfiled.text = to that.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.