Actionscript Dynamic Text

  • ScienceOfSpock
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1892
  • Loc: Las Vegas

Post 3+ Months Ago

Hi yall,
I'm having a problem generating dynamic text fields in Flash MX Pro. I know how to create a text field through actionscript, the problem is that I have to create an arbitrary number of them.
I'm working on a mapping application that draws vector maps, and that part is all done and working properly.
The map data comes in via XML and includes points of interest that contain x and y coordinates and a name. I have the point plotting done, now I just need to add the labels, and this is where I run into a dead end. I know how to create a dynamic text field with actionscript, the problem is that I have to generate an unknown number of them. I can even do this by looping over the xml data and creating a text field for each point of interest:
Code: [ Select ]
for ( i = 0; i < 10; i++ )
{
    createTextField("textbox"+i, i, -120, -40+ ( i*10 ), 240, 80);
}
  1. for ( i = 0; i < 10; i++ )
  2. {
  3.     createTextField("textbox"+i, i, -120, -40+ ( i*10 ), 240, 80);
  4. }

The problem with that is that the textfields get created, but they're not accessible now because they've been created with a variable as part of the name ("textbox"+i), and I can't assign text to them. I have tried using eval() but apparently it doesn't work in flash like it does in javascript or php because the text property for each textfield is blank. (see below code):
Code: [ Select ]
for ( i = 0; i < 10; i++ )
{
    createTextField("textbox"+i, i, -120, -40.0+(i*10), 240, 80);
    eval ( "textbox"+i+".selectable = false;" );
    eval ( "textbox"+i+".text = 'label';" );
    trace ( " text " + i + ": " + eval ( "textbox"+i+".text" ) );
}
  1. for ( i = 0; i < 10; i++ )
  2. {
  3.     createTextField("textbox"+i, i, -120, -40.0+(i*10), 240, 80);
  4.     eval ( "textbox"+i+".selectable = false;" );
  5.     eval ( "textbox"+i+".text = 'label';" );
  6.     trace ( " text " + i + ": " + eval ( "textbox"+i+".text" ) );
  7. }


How can I create an arbitrary number of text fields in flash? The code above is just using a for loop to count to 10, but the actual data will be coming from xml and there is no way to know how many labels there will be. This is driving me nuts. Any help would be appreciated.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • IceCold
  • Guru
  • Guru
  • User avatar
  • Posts: 1254
  • Loc: Ro

Post 3+ Months Ago

hehehe, very that's easy.
though i'd suggest to create one movieclip with a text field in it, and export it for actionscript; this way you can attach it how many times you want and also set it's default attributes (font, font size, font color) and embed it as well.
anyway, about your problem:
Code: [ Select ]
for ( i = 0; i < 10; i++ )
{
  this.createTextField("textbox"+i, i, -120, -40.0+(i*10), 240, 80);
  this["textbox"+i].selectable = false;
  this["textbox"+i].text = "label";
  trace ( " text " + i + ": " + this["textbox"+i].text );
}
  1. for ( i = 0; i < 10; i++ )
  2. {
  3.   this.createTextField("textbox"+i, i, -120, -40.0+(i*10), 240, 80);
  4.   this["textbox"+i].selectable = false;
  5.   this["textbox"+i].text = "label";
  6.   trace ( " text " + i + ": " + this["textbox"+i].text );
  7. }

with attaching movies would be like this: Place a text field on the stage, name it's instance to txt. Select it and press F8 to make it a movieclip, name it textbox. Then delete it from the stage and go to the library, right click on it -> linkage export for actionscript.
Code: [ Select ]
this.createEmptyMovieClip("mcTexts", this.getNextHighestDepth());
var crtMC:MovieClip;
for ( i = 0; i < 10; i++ )
{
  crtMC = mcTexts.attachMovie("textbox", "textbox"+i, i);
  crtMC._x = -120;
  crtMC._y = -40.0+(i*10);
  crtMC.txt.selectable = false;
  crtMC.txt.text = "label";
  trace ( " text " + i + ": " + crtMC.txt.text );
}
  1. this.createEmptyMovieClip("mcTexts", this.getNextHighestDepth());
  2. var crtMC:MovieClip;
  3. for ( i = 0; i < 10; i++ )
  4. {
  5.   crtMC = mcTexts.attachMovie("textbox", "textbox"+i, i);
  6.   crtMC._x = -120;
  7.   crtMC._y = -40.0+(i*10);
  8.   crtMC.txt.selectable = false;
  9.   crtMC.txt.text = "label";
  10.   trace ( " text " + i + ": " + crtMC.txt.text );
  11. }
  • ScienceOfSpock
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1892
  • Loc: Las Vegas

Post 3+ Months Ago

Thanks for the help. That was easier than I expected.

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 4 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.