I’m fairly new to Actionscript 3, I’m starting to get the hang of things.
So here’s my question:
I have 40 dynamic text boxes inside a moveclip instanced game, their instance name is like this
unit0
unit1
unit2
unit3
…
unit39
I’d like to create a loop that will assign text to each one of these. I think I need to use an array, but I’m not quite sure how I’d do that.
I’ve got this right now:
for (var i:Number = 0; i < 40; i++) {
game.unit*.text = "test text";
}
Obviously, this didn’t work, and I didn’t really expect it to.
Later on, I’ll have each text box say something else, which it will be reading from an xml file, but first, I want to figure this much out.
Thanks. I hope I explained my question well enough
Thanks for the quick reply. I actually just dragged each of the text boxes onto the stage (meaning I didn’t create them in actionscript). I wasn’t sure if I needed an array or not, so no need to go out of the way to make one.
The first bit of code you gave me is returning this:
TypeError: Error #1010: A term is undefined and has no properties.
at game_fla::MainTimeline/game_fla::frame1()
Right now, I have a movie clip called game (which I have the class called “gameClass”) which I added to the stage from the library with actionscript. Inside the game movie clip are 40 dynamic text boxes. I didn’t create these text boxes with actionscript.
So heres the code I have:
var game:MovieClip = new gameClass();
stage.addChild(game);
game.x = 400;
game.y = 400;
for (var i:Number = 0; i < 40; i++) {
game["unit" + i].text = "test text";
}
And thats all the code I have right now.
This is outputting:
TypeError: Error #1010: A term is undefined and has no properties.
at game_fla::MainTimeline/game_fla::frame1()
That should work. The error you’re getting typically means that you’re accessing the properties of an undefined object - so you just asked it something like “text41.text” and it doesn’t know how to handle it.
Try cutting the loop size down to like 10, see if that works. Also, you’re sure you started at 0, right? Do you have exactly 40 objects?
[quote=Anogar;2338365]That should work. The error you’re getting typically means that you’re accessing the properties of an undefined object - so you just asked it something like “text41.text” and it doesn’t know how to handle it.
Try cutting the loop size down to like 10, see if that works. Also, you’re sure you started at 0, right? Do you have exactly 40 objects?[/quote]
Thanks so much! Some how I skipped unit3. =/
Sorry, stupid mistake. The whole thing is working fine now.
Thanks for the help. I may run into more problems when I try to get this to work with an xml file.
I’ll play around with it for a bit. Its a learning process
var myNewXML:CoreXML = new CoreXML("pathToMyFile.xml");
… and that pretty much takes care of the whole thing. Saves me a lot of time, since I do a ton of work with XML. It dispatches a COMPLETE event when it’s done loading, so you can do something like this:
var myNewXML:CoreXML = new CoreXML("pathToMyFile.xml");
///
myNewXML.addEventListener(Event.COMPLETE, myFunction);
///
function myFunction(e:Event):void
{
trace(e.currentTarget.xmlData) //returns your XML data
}
I’m doing something similar. All of my text is pulling from my XML just fine, my loop seems to be working ok, however it’s displaying everything in a single column. What I’d like to do is have multiple columns of some number. I’ve been searching for what this is called, but haven’t been able to find anything, cans someone give me a push start. My code is below:
for (var a:Number = 0; a < 35; a++){
clist_mc = new Clist();
addChild(clist_mc);
clist_mc.y = a * 20;
clist_mc.clist_tf.htmlText = xmlList[a]
}