Judging by the amount of keywords my other AS3 post picked up like “Creating a new text loop in AS3″, I was obliged to answer.
Well, creating textfields in a loop is quite simple. Lets define a bare-minimum TextFormat outside of our loop (unless it needs to change per iteration).
var textFormat:TextFormat = new TextFormat("verdana", 11)
By the way, lets use our little trick we learned about assigning dynamic variable names to movieclips here because the same issue applies: we want access to our textfields in case we want to modify them later. The problem is that after this loop, we have no way of accessing them!
Lets create an array to store all of our textfield references.
var textArray:Array = new Arrray()
and now the loop:
for (var i:int; i < 3; i++) {
var textField:TextField = new TextField()
textField.DefaultTextFormat = textFormat
textField.text = 'This is my TextField' + i
textField.x = 50*i
addChild(textField)
textArray.push(textField)
}
this would create 3 textFields with text: “This is my TextField0″, 1, 2.
We can now access those textfields (say the x position was too arbitrary) with our textArray.
textArray[0] = our first textField, textArray[1] = our second one, etc.
textArray[0].x = 500 // to access each object
Here’s a tip for having more useable text in the textFields. After all, the same thing plus a number is quite boring.
We can make another Array that holds the text each textField is supposed to have.
How about:
var text:Array = new Array('First TextField','TextField Two', 'Anything, really')
Now we can run through our loop again according to how many things are defined in our Array called text.
for (var i:int; i < text.length; i++) {
var textField:TextField = new TextField()
textField.defaultTextFormat = textFormat
textField.text = text[i]
textField.x = 50*i
addChild(textField)
textArray.push(textField)
}
This time, our loop will conveniently create as many textFields as you put in the “text” array. In retrospect, naming the textfield reference array textArray was a bad idea, as it forced me to use an uninformative name for the second array. It probably should have been something like: tfReferenceArray, and tfTextArray.
Hope something was useful. There are endless applications for this stuff.
12 responses so far ↓
Russ // May 16, 2008 at 4:07 am |
Thank you so much. This is exactly what I needed.
etuan // July 3, 2008 at 11:34 pm |
Seems like I may have been able to figure out myself but you just saved me the hours of trial and error that it would have taken me. ¡Muchas Gracias Amigo!
Johnny // July 9, 2008 at 10:41 am |
So there is no way to use:
addChild (this["textField"] + i); //or something similar to omit the arrays, and afterwards reference to the textField or MovieClip by variable name.
Great job with both tutorials, I see that AS3 needs often more typing then AS1, but AVM2 must compensate me that. Thanx for help!
Yuji // July 9, 2008 at 11:19 am |
Hey Johnny,
I don’t know much about AS3 myself, but from looking around google for a long time, it doesn’t seem like you can dynamically create variable names for them.
Please let me know if you find out otherwise!
-Yuji
Joren // July 18, 2008 at 7:32 am |
Awesome dood. I was diggin around trying to do this but i didnt know about the push method or whatever. Thanks for saving me unknown hours of messing with code. Now I just need to fit everything in a dynamic scroll bar. Thanks.
Alpesh Vaghasiya // July 24, 2008 at 11:13 pm |
let me know if you know any other method. it’s not permenet solution. if i want to do some process on that dynamic text fields then how can we do it.
Yuji // July 24, 2008 at 11:20 pm |
Alpesh Vaghasiya: Thanks for the comment. If you want to do something to those fields, work on the TextField Array (textArray in the example).
textArray[0] = first TF, textArray[1] = second TF, etc.
Jerome // September 19, 2008 at 1:32 am |
just to let you know that this echo flash cs3 errors all the way :
for (var i:int; i < 3; i++) {
var textField:TextField = new TextField()
textField.DefaultTextFormat = textFormat
textField.text = ‘This is my TextField’ + i
textField.x = 50*i
addChild(textField)
textArray.push(textField)
}
Yuji // September 19, 2008 at 2:07 am |
Hi Jerome, you are right. DefaultTextFormat should be defaultTextFormat, and the reason it won’t work if you copy and paste is because wordpress.com autoformats ‘ to `
Yuji // September 19, 2008 at 2:09 am |
er, the wordpress autoformatted ‘ is a ` if pasted into something.
jav666 // September 26, 2009 at 9:57 pm |
thx for the idea!
vfxmograf // November 25, 2009 at 5:25 am |
this works on papervision cubes too!!