Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to add textfields to an array

Contributor ,
Apr 21, 2023 Apr 21, 2023

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?

 

870
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Apr 21, 2023 Apr 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

Translate
Community Expert , Apr 21, 2023 Apr 21, 2023

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

...
Translate
Community Expert ,
Apr 21, 2023 Apr 21, 2023

you have several errors. use:

 

var myLabels:Array = new Array();

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

 

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 21, 2023 Apr 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 21, 2023 Apr 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 21, 2023 Apr 21, 2023

The myLabels Array is declared in frame1, then I'm attempting to add its members in frame2 in a loop, where i is incrementing from 0-3. In that loop:

var myLabels[i] = new TextField();

is what's giving the syntax error:
1086: Syntax error: expecting semicolon before leftbracket.

The error goes away if I comment out that line.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 21, 2023 Apr 21, 2023

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 21, 2023 Apr 21, 2023
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines