# Dynamic Text Question

**URL:** <https://forum.kirupa.com/t/dynamic-text-question/262560>\
**Category:** Uncategorized\
**Created:** [June 6, 2008, 9:46pm UTC](https://forum.kirupa.com/t/dynamic-text-question/262560 "2008-06-06T21:46:35Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![imbored531](https://avatars.discourse-cdn.com/v4/letter/i/ee59a6/32.png) [@imbored531](https://forum.kirupa.com/u/imbored531)\
**Post date:** [June 6, 2008, 9:46pm UTC](https://forum.kirupa.com/t/dynamic-text-question/262560/1 "2008-06-06T21:46:35Z")

</div>

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:

```auto

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 🙂

---

<div class="post-metadata">

**Author:** ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)\
**Post date:** [June 6, 2008, 9:53pm UTC](https://forum.kirupa.com/t/dynamic-text-question/262560/2 "2008-06-06T21:53:48Z")

</div>

The syntax would be:

```auto

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

```

---

<div class="post-metadata">

**Author:** ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)\
**Post date:** [June 6, 2008, 9:55pm UTC](https://forum.kirupa.com/t/dynamic-text-question/262560/3 "2008-06-06T21:55:34Z")

</div>

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

```auto

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.

---

<div class="post-metadata">

**Author:** ![imbored531](https://avatars.discourse-cdn.com/v4/letter/i/ee59a6/32.png) [@imbored531](https://forum.kirupa.com/u/imbored531)\
**Post date:** [June 6, 2008, 10:05pm UTC](https://forum.kirupa.com/t/dynamic-text-question/262560/4 "2008-06-06T22:05:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)\
**Post date:** [June 6, 2008, 10:13pm UTC](https://forum.kirupa.com/t/dynamic-text-question/262560/5 "2008-06-06T22:13:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![imbored531](https://avatars.discourse-cdn.com/v4/letter/i/ee59a6/32.png) [@imbored531](https://forum.kirupa.com/u/imbored531)\
**Post date:** [June 6, 2008, 10:25pm UTC](https://forum.kirupa.com/t/dynamic-text-question/262560/6 "2008-06-06T22:25:45Z")

</div>

> [@Anogar;2338361](#):
>
> 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:

```auto

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

---

<div class="post-metadata">

**Author:** ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)\
**Post date:** [June 6, 2008, 10:34pm UTC](https://forum.kirupa.com/t/dynamic-text-question/262560/7 "2008-06-06T22:34:27Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![imbored531](https://avatars.discourse-cdn.com/v4/letter/i/ee59a6/32.png) [@imbored531](https://forum.kirupa.com/u/imbored531)\
**Post date:** [June 6, 2008, 10:37pm UTC](https://forum.kirupa.com/t/dynamic-text-question/262560/8 "2008-06-06T22:37:54Z")

</div>

[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 😉

---

<div class="post-metadata">

**Author:** ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)\
**Post date:** [June 6, 2008, 10:49pm UTC](https://forum.kirupa.com/t/dynamic-text-question/262560/9 "2008-06-06T22:49:03Z")

</div>

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](http://code.google.com/p/ragona-as3utils/source/browse/trunk/com/rragona/utils/coreXML/CoreXML.as)

The usage is:

```auto

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:

```auto

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
}

```

---

<div class="post-metadata">

**Author:** ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)\
**Post date:** [June 6, 2008, 10:49pm UTC](https://forum.kirupa.com/t/dynamic-text-question/262560/10 "2008-06-06T22:49:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![imbored531](https://avatars.discourse-cdn.com/v4/letter/i/ee59a6/32.png) [@imbored531](https://forum.kirupa.com/u/imbored531)\
**Post date:** [June 6, 2008, 10:57pm UTC](https://forum.kirupa.com/t/dynamic-text-question/262560/11 "2008-06-06T22:57:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![imbored531](https://avatars.discourse-cdn.com/v4/letter/i/ee59a6/32.png) [@imbored531](https://forum.kirupa.com/u/imbored531)\
**Post date:** [June 7, 2008, 12:05am UTC](https://forum.kirupa.com/t/dynamic-text-question/262560/12 "2008-06-07T00:05:52Z")

</div>

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

```auto

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:

```auto

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

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

```

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

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![imbored531](https://avatars.discourse-cdn.com/v4/letter/i/ee59a6/32.png) [@imbored531](https://forum.kirupa.com/u/imbored531)\
**Post date:** [June 7, 2008, 12:23am UTC](https://forum.kirupa.com/t/dynamic-text-question/262560/13 "2008-06-07T00:23:25Z")

</div>

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:

```auto

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?

---

<div class="post-metadata">

**Author:** ![Felixz](https://avatars.discourse-cdn.com/v4/letter/f/ecb155/32.png) [@Felixz](https://forum.kirupa.com/u/Felixz)\
**Post date:** [June 7, 2008, 9:49am UTC](https://forum.kirupa.com/t/dynamic-text-question/262560/14 "2008-06-07T09:49:30Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![imbored531](https://avatars.discourse-cdn.com/v4/letter/i/ee59a6/32.png) [@imbored531](https://forum.kirupa.com/u/imbored531)\
**Post date:** [June 7, 2008, 2:51pm UTC](https://forum.kirupa.com/t/dynamic-text-question/262560/15 "2008-06-07T14:51:41Z")

</div>

[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.

---

<div class="post-metadata">

**Author:** ![flashdaddy](https://avatars.discourse-cdn.com/v4/letter/f/e36b37/32.png) [@flashdaddy](https://forum.kirupa.com/u/flashdaddy)\
**Post date:** [June 7, 2008, 11:41pm UTC](https://forum.kirupa.com/t/dynamic-text-question/262560/16 "2008-06-07T23:41:37Z")

</div>

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:

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

```
