Dynamic Text Question

Hey,

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 :slight_smile:

The syntax would be:


game["unit" + i].text = "test text";

Oops. As far as the array goes, what code are you using to make the text boxes? It should be something like this:


var textArray:Array = [];

for (var z:int = 0; z < 40; z ++)
{
     var newBox:TextField = new TextField()
     textArray.push(newBox);
}

That’ll put all the objects into the textArray as they’re created.

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()

Thanks for the help

You put it inside the existing for loop, right? The original code you posted was very nearly right.

Correct, its inside the for loop.

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 :wink:

Once you get the XML coming in you might want to consider generating the text boxes with code, it’ll make it a lot easier to make edits.

By the way, I use this class for all my AS3 XML loading. It just cuts a few lines of code out and makes things a lot easier.

http://code.google.com/p/ragona-as3utils/source/browse/trunk/com/rragona/utils/coreXML/CoreXML.as

The usage is:


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
}

(By the way, I wrote the class, so if it has any weird errors feel free to come yell at me.)

Thanks, I’ll give it a try. Should save me some time in the long run.

All right, I have my xml working correctly. Here’s the code I have right now:


for (var i:int = 0; i < 40; i++) {
   var nameArr:Array = new Array();
   nameArr* = mainXML..unit.(@id=="0").name.text();
   board["unit" + i].text = nameArr*;
}

My XML is layed out like this:


<data>
  <units>
    <unit id="0">
      <name>Name One</name>
    </unit>
    <unit id="1">
      <name>Name Two</name>
    </unit>
    ...
    <unit id="39">
      <name>Name Forty</name>
    </unit>
  </units>
</data>

My actionscript right now will only read from id 0. Does anybody know how I could make it read each one?

Something like this:

   nameArr* = mainXML..unit.(@id==*****).name.text(); 

That was just a guess and it won’t work.

Thanks for the help!

All right, I figured something out… If I get rid of the double equal sign, it will be able to read each one. So here’s what I’ve got:


for (var i:int = 0; i < 40; i++) {
   var nameArr:Array = new Array();
   nameArr* = mainXML..unit.(@id=*).name.text();
   board["unit" + i].text = nameArr*;
}

Here’s the problem: In each text box, it says the same thing:
Name OneName TwoName Three …

If I trace nameArr anything below 39 it will return undefined. If I trace(nameArr[39]) it will return every name in my XML file.

I think I need to set this up some other way…

Any ideas?

for (var i:int = 0; i < mainXML.unit.length(); i++) {
   board["unit" + i].text = mainXML.unit*.name.text();
}

[quote=Felixz;2338497] ActionScript Code:
[LEFT][COLOR=#0000FF]for[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#000000]var[/COLOR] i:[COLOR=#0000FF]int[/COLOR] = [COLOR=#000080]0[/COLOR]; i < mainXML.[COLOR=#000080]unit[/COLOR].[COLOR=#0000FF]length[/COLOR]COLOR=#000000[/COLOR]; i++[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
board[COLOR=#000000][[/COLOR][COLOR=#FF0000]“unit”[/COLOR] + i[COLOR=#000000]][/COLOR].[COLOR=#0000FF]text[/COLOR] = mainXML.[COLOR=#000080]unit[/COLOR][COLOR=#000000][[/COLOR]i[COLOR=#000000]][/COLOR].[COLOR=#0000FF]name[/COLOR].[COLOR=#0000FF]text[/COLOR]COLOR=#000000[/COLOR];
[COLOR=#000000]}[/COLOR]
[/LEFT]

[/quote]

Thanks! Everything’s working great now. I just assumed I had to use the parentheses and the at symbol to get data from an attribute.

Hello All,

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]
    }