dynamically create multiple TextFields based on ar

2019-09-17 04:16发布

问题:

I've been looking around all day and havent had any luck finding a solution for this.

What im looking to do is dynamically create TextFields based on my array.length. So if I have 3 strings in my array then 3 TextFields with the array text needs to be created.

I've managed to actually create TextFields based on the array.length - however afterwards I dont know how to reference them individually, to lets say re-position x, y for array[1]. I've tried saving the Textfields in another array by .push method, but can't seem to reference them correctly.

Any suggestions?

//Create textfields based on data in Array - in this case 3 textfields
var textArray:Array = new Array('First TextField','TextField Two','Anything, really');

//Array to .push "save" created textfields
var referenceArray:Array = new Array();

// Creating font instance
var garageInstance:Font = new garage();

var myFormat:TextFormat = new TextFormat();

//Embedding font
myFormat.font = garageInstance.fontName;
myFormat.color = 0xFFFFFF;
myFormat.size = 46;
myFormat.align = TextFormatAlign.CENTER;

for (var i:int; i < textArray.length; i++)
{
//Creating the textfield object and naming it "myTextField2"
var myTextField2:TextField = new TextField();

myTextField2.defaultTextFormat = myFormat;
myTextField2.width = 930;
myTextField2.embedFonts = true;
myTextField2.multiline = true;
myTextField2.wordWrap = true;
myTextField2.selectable = false;
myTextField2.htmlText = textArray[i];

myTextField2.autoSize = TextFieldAutoSize.CENTER;

//Here we add the new textfield instance to the stage with addchild()
addChild(myTextField2);

//Saving textfield into array   
referenceArray.push(myTextField2);

}

回答1:

I haven't seen anything weird with your code, everything just works perfectly. If you don't see any result, maybe your background is set to white, or you have a white object already added to the stage. In this case you won't see anything, as your text's color is set to white (0xffffff). If you set it to be black (0x000000), for example, then you will see the nice result.

If this is not the case, did you reference your font correctly? If using the Adobe IDE, right click on the font in the library and select Export for ActionScript.

The array referencing you are using is perfect. Put this code after your script:

trace(referenceArray[0], referenceArray[0].text);

and you will see, it traces the result:

[object TextField] First TextField

So the output is fine, your code can see the instance, therefore it can read it's text property and it is correct.

If you want to set the textfield's coordinates dynamically, just put

myTextField2.y = i * 50;

in the for loop. This will place each textield as follows: 0, 50, 100 etc.

You can also play with the x coordinate.



回答2:

Could try an Array filled with Objects that define the properties of each text field..

Your objects could define any properties you like - inbuilt or custom-made (if you extend TextField with your own class and add that instead).

Example: array:

var ar:Array = [
{
    text: "text field 1",
    y: 100,
    x: 20
},
{
    text: "text field 2",
    y: 200,
    x: 10,
    alpha: 0.5 // demonstrating that you can define any properties in the object
}];

And the for loop that creates your fields:

/**
 * Iterate through ar and create textfields with defined properties
 */
var i:Object;
for each(i in ar)
{
    var t:TextField = new TextField();

    /**
     * Text format things here
     */

    var j:String;
    for(j in i)
    {
        t[j] = i[j];
    }

    addChild(t);
}