0

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

}

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Hey anemgyenge, Thanks a lot for your answer. I could do myTextField2.y = i * 50; however that would just, do 0, 50, 100 etc .. However lets say I want to position the text in: referenceArray[0].text x = 0 and referenceArray[1].text = x = 500 etc. In the real setup, im tweening the textfields from a start, mid and end point, so im looking to be able to reference each textfield invidually - so that I can place the textfields different places
Then you can create another array, like positions, and you type each textfield position there and in the loop: textfield[i].x = positions[i]. So you mean to position the whole textfield, right?
Hmm yea, that could actually sound like an idea. Guess I didnt manage to tell the final twist about this - and that is that I have two containers, which im adding adding swapping the different textfields between. I have a starting text on screen - this will tween out and the second container tween in - while container1 resets to default "out" position and next text in the array is swapped into container1.. So guess for the swapping part I need to be able to reference the textfield directly somehow in my slide function
In this case I think you can accept the answer as an answer :) If you have more twists and questions, not included in the title, give it a try and ask it in a new question :)
You're totally right. Thanks a lot for contributing anemgyenge :)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.