Skip to main content
Inspiring
April 21, 2023
Answered

How to add textfields to an array

  • April 21, 2023
  • 1 reply
  • 986 views

What is the correct method to add TextFields to an Array? When I declare an Array for:

var myLabels:Array = new Array();

then attempt to populate it with TextFields, using:
 var myLabels[i]:TextField = new TextField();

I get a syntac error. Is there another step, or a different method to use to assign it?

 

    This topic has been closed for replies.
    Correct answer kglad

    Ok, I see. I didn't need the extra var definition. It works after I noticed you omitted that, and I took it out. Thanks.


    you're welcome.

     

    and, if you read the error message carefully, it's pinpointing the error for you.  ie, there's only one left bracket in your posted code.  the actual problem (var) isn't explained because animate thinks you mean something like:

     

    var xyz; myLabels[i] = new TextField();

     

    and is unhappy it sees no xyz;

     

    but, like most compilers, it is perfectly happy with (and allows multiple statements) on one line. only we humans have trouble reading code with multiple statements on the same line.

    1 reply

    kglad
    Community Expert
    Community Expert
    April 21, 2023

    you have several errors. use:

     

    var myLabels:Array = new Array();

    var i:int = 0;
    myLabels[i]=new TextField();

     

     

     

    hclmed0Author
    Inspiring
    April 21, 2023

    The i value is coming from a loop, so its not undefined. But even:

    var myLabels[0] = new TextField();

    gives the error:
    1086: Syntax error: expecting semicolon before leftbracket.

    kglad
    Community Expert
    Community Expert
    April 21, 2023

    then use:

     

    var myLabels:Array = new Array();

    //var i:int = 0;
    myLabels[i]=new TextField();

     

    ie, this is problematic:

     

    var myLabels[i]=new ANYTHING();  // is going to trigger an error