flash (macromedia):
I'm trying to use the following code to format text fields with the instance names test0, test1, test2, etc. inside the "slider_mc" movie instance. The variable "i" is dynamically changed to 0,1,2,3,etc. through a loop. The variable "txtFmt" represents the TextFormat that the text fields should adhere to. _root.setTextFormat(txtFmt); The code above does not work, but the following code does: _root.setTextFormat(txtFmt); However, the code above is not dynamic. In other words, it works with an integer, but not a variable. Any suggestions?
It shouldn't make a difference. Try this: trace(_root.slider_mc); trace(i); trace(_root.slider_mc); Perhaps you are incorrectly referencing it, or i has an value not in the range... Could you perhaps show the entire loop? By the way, there's no need to use _root since there's nothing inside that, that's going to dynamically change.
The problem seems to be the "i" variable. It was originally created outside the while loop that the posted code falls within. If I create a new variable inside the loop and use it in place of the "i" variable, everything works. However, I need to access the "i" variable. Is there a way to correctly do this?
Here is the code: function startMovie(success) { if (success == true) { var childItems:Array = mx.xpath.XPathAPI.selectNodeList(this.firstChild,"/rss/channel/item/title"); var descNodes:Array = mx.xpath.XPathAPI.selectNodeList(this.firstChild,"/rss/channel/item/description" ); count = childItems.length; z = 0; i = 0; while (i < count){ trace(_root.slider_mc.createTextField("test" + i, _root.slider_mc.getNextHighestDepth(), z, 10, 200, 400)); z = z + 200; var rss:String = "test" + i; var txtFmt:TextFormat = new TextFormat(); txtFmt.bold = false; txtFmt.size = 30; //This is where the problem is. _root.slider_mc.setTextFormat(txtFmt); trace(_root.slider_mc.text = childItems.firstChild.nodeValue); _root.slider_mc.wordWrap = true; _root.slider_mc.multiline = true; _root.slider_mc.test0.gridFitType = "pixel"; _root.slider_mc.test0.align = "left"; i = i + 1; } } else { this.createTextField("error", 10, 10, 10, 320, 100); error.text = "Didn't work"; } } function startMovie(success) { if (success == true) { var childItems:Array = mx.xpath.XPathAPI.selectNodeList(this.firstChild,"/rss/channel/item/title"); var descNodes:Array = mx.xpath.XPathAPI.selectNodeList(this.firstChild,"/rss/channel/item/description" ); count = childItems.length; z = 0; i = 0; while (i < count){ trace(_root.slider_mc.createTextField("test" + i, _root.slider_mc.getNextHighestDepth(), z, 10, 200, 400)); z = z + 200; var rss:String = "test" + i; var txtFmt:TextFormat = new TextFormat(); txtFmt.bold = false; txtFmt.size = 30; _root.slider_mc["test" + i].setTextFormat(txtFmt); trace(_root.slider_mc[rss].text = childItems[i].firstChild.nodeValue); _root.slider_mc[rss].wordWrap = true; _root.slider_mc[rss].multiline = true; _root.slider_mc.test0.gridFitType = "pixel"; _root.slider_mc.test0.align = "left"; i = i + 1; } } else { this.createTextField("error", 10, 10, 10, 320, 100); error.text = "Didn't work"; } }
Here is the code (the previous code wasn't the neatest): function startMovie(success) { if (success == true) { var childItems:Array = mx.xpath.XPathAPI.selectNodeList(this.firstChild,"/rss/channel/item/title"); var descNodes:Array = mx.xpath.XPathAPI.selectNodeList(this.firstChild,"/rss/channel/item/description" ); count = childItems.length; z = 0; i = 0; while (i < count){ trace(_root.slider_mc.createTextField("test" + i, _root.slider_mc.getNextHighestDepth(), z, 10, 200, 400)); z = z + 200; var rss:String = "test" + i; var txtFmt:TextFormat = new TextFormat(); txtFmt.bold = false; txtFmt.size = 30; //PROBLEM OCCURS HERE _root.slider_mc.setTextFormat(txtFmt); trace(_root.slider_mc.text = childItems.firstChild.nodeValue); _root.slider_mc.wordWrap = true; _root.slider_mc.multiline = true; _root.slider_mc.test0.gridFitType = "pixel"; _root.slider_mc.test0.align = "left"; i = i + 1; } } else { this.createTextField("error", 10, 10, 10, 320, 100); error.text = "Didn't work"; } }
All you need to do is place setTextFormat AFTER you set the text, or use setNewTextFormat instead. By the way, I believe the following code will work but a bit simplified. function startMovie(success) { if (success) { var childItems:Array = mx.xpath.XPathAPI.selectNodeList(this.firstChild, "/rss/channel/item/title"); var descNodes:Array = mx.xpath.XPathAPI.selectNodeList(this.firstChild, "/rss/channel/item/description"); var txtFmt:TextFormat = new TextFormat(); txtFmt.bold = false; txtFmt.size = 30; for (var i = 0; i<childItems.length; i++) { var my_txt = _root.slider_mc.createTextField("test"+i, _root.slider_mc.getNextHighestDepth(), i*200, 10, 200, 400); my_txt.setNewTextFormat(txtFmt); my_txt.text = childItems[i].firstChild.nodeValue; my_txt.wordWrap = true; my_txt.multiline = true; my_txt.gridFitType = "pixel"; my_txt.align = "left"; } } else { this.createTextField("error", 10, 10, 10, 320, 100); error.text = "Didn't work"; } }
Don't see what you're looking for? Try a search.
|