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

How to add textfields to an array

Participant ,
Apr 21, 2023 Apr 21, 2023

Copy link to clipboard

Copied

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?

 

Views

582

Translate

Translate

Report

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

Votes

Translate

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

...

Votes

Translate

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

Copy link to clipboard

Copied

you have several errors. use:

 

var myLabels:Array = new Array();

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

 

 

 

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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