Link to home
Start Free TrialLog in
Avatar of worked
workedFlag for United States of America

asked on

How do I modify the first item in an xmlList?

Hey there-

I have a very simple for loop that parses an xml doc into textfields.  Each textfield is added to a container within the loop for animation purposes later.  Question.  How do I modify the first item in the xmlList so that it displays an image instead of text?  My code is below.  Thanks! Any help is appreciated.
ACTIONSCRIPT 3:
for (var i:int = 0; i < xmlList.length(); i++) {
	var container:MovieClip = new MovieClip();
	addChild(container);
	
	var tf:TextField = new TextField();
	tf.htmlText = xmlList[i].title;
	//not working - tf.htmlText = '<img src="'+ xmlList[0].title +'">';
	tf.autoSize = TextFieldAutoSize.CENTER;
	tf.x = startPos;
	tf.y = (stage.stageHeight / 2) - (tf.height / 2);
	widthValue = tf.width;
 
	container.path = xmlList[i].path;
	container.addChild(tf);
	container.addEventListener(MouseEvent.CLICK, onContainerDown);
	startPos = startPos + (widthValue + spacer);
}
 
XML:
<?xml version='1.0' encoding='ISO-8859-1' ?>
<bc>
<item>
<title>image.gif</title>
<path>/</path>
</item>
<item>
<title>000000</title>
<path>/bc</path>
</item>
<item>
<title>000000</title>
<path>/bc/bc_1</path>
</item>
</bc>

Open in new window

Avatar of worked
worked
Flag of United States of America image

ASKER

Also, if it is easier to create the graphic in actionscript and replace the first item in the xmlList, then by all means please show me that method as well or instead.  My code below attempts this but displays [object Sprite] in the textfield instead of the graphic.  Any help is appreciated!  Thanks!
ACTIONSCRIPT:
var icon:Sprite = new Sprite();
icon.graphics.lineStyle(1, 0x000000);
icon.graphics.moveTo(0, 0);
icon.graphics.lineTo(-6, (-stage.stageHeight/2 + 2));
icon.graphics.moveTo(0, 0);
icon.graphics.lineTo(-6, stage.stageHeight/2 - 2);
icon.x = startPos - (spacer / 2);
icon.y = stage.stageHeight / 2;
for (var i:int = 0; i < xmlList.length(); i++) {
        var container:MovieClip = new MovieClip();
        addChild(container);
        
        var tf:TextField = new TextField();
        tf.htmlText = xmlList[i].title;
        // not working - tf.htmlText = '<img src="'+ xmlList[0].title +'">';
        // replace first item in list
        // xmlList[0].title = hi;  // this outputs [object Sprite] instead of the graphic.
        tf.autoSize = TextFieldAutoSize.CENTER;
        tf.x = startPos;
        tf.y = (stage.stageHeight / 2) - (tf.height / 2);
        widthValue = tf.width;
 
        container.path = xmlList[i].path;
        container.addChild(tf);
        container.addEventListener(MouseEvent.CLICK, onContainerDown);
        startPos = startPos + (widthValue + spacer);
}

Open in new window

try this:

You can escape the ' and " characters by putting a \ before them...

DM
tf.htmlText = "<img src=\""+ xmlList[0].title +"\" >";

Open in new window

Avatar of worked

ASKER

I had to place a dynamic textfield on the stage and load the graphic into that.  I also used a nested for loop to display and position this graphic among the other textfields.  

Question.  Why can't I load a graphic into a textfield that is dynamically created, yet can load one into a dynamic textfield drawn on the stage?  

For instance, where textList[i].link = image.gif


This WON'T load a graphic into a textfield:
 
var img:TextField;
for (var i:uint = 0; i < textList.length(); i++) {
	img = new TextField();
	img.htmlText = "<a href=\"" + textList[i].link + "\"><img src=\"" + textList[i].title + "\"></a>";
	img.x = 0;
	img.y = 0;
        addChild(img);
}
 
 
However, this WILL load the graphic:
 
for (var i:uint = 0; i < textList.length(); i++) {
	//load graphic into dynamic textfield on stage... named img
	img.htmlText = "<a href=\"" + textList[i].link + "\"><img src=\"" + textList[i].title + "\"></a>";
	img.x = 0;
	img.y = 0;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dreammonkey
Dreammonkey
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial