Not signed in (Sign In)

Vanilla 1.1.4 is a product of Lussumo. More Information: Documentation, Community Support.


    • CommentAuthorwebguy
    • CommentTimeNov 5th 2006
     
    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:


    for ( i = 0; i < 10; i++ )
    {
       createTextField("textbox"+i, i, -120, -40+ ( i*10 ), 240, 80);
    }


    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.


    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" ) );
    }


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


    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 );
    }


    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.


    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 );
    }
    • CommentAuthorwebguy
    • CommentTimeNov 5th 2006
     
    Thanks for the help. That was easier than I expected.