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

Button instance names stored in array not working.

Community Beginner ,
Apr 28, 2022 Apr 28, 2022

Copy link to clipboard

Copied

Newbie here. 

 

I created an array containing the instance names of 6 buttons - for example:

 

window.myBtns = ["a1_mc","a2_mc","a3_mc","a4_mc","b1_mc","b2_mc","b3_mc","b4_mc"];

 

When if go to loop through the array to pull the button instance names and assign the nested text field a value from another array I receive an error message saying "' is undefined. Here's my loop:

 

for (let i = 0; i < selQues.length; i++) {
  switch (i) {
    case 0:
      for (let k = 0; k < selQuesSets[i].length; k++) {
        var myMC = queABtns[k];

        myBtns[k].myTxt_txt.text = selQuesSets[0][k].value;.
      }
      break;
  }
}

 

What am I doing wrong?

 

Another question. If I declare my array on frame 0, its undefined on frame 25 where I need to use it. How do I declare an array on frame 0 that's accessable throughout the timeline? Is it because the buttons are not used until frame 25?

Views

87

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
LEGEND ,
Apr 28, 2022 Apr 28, 2022

Copy link to clipboard

Copied

LATEST

What on Earth is that switch supposed to accomplish?

 

myBtns[k] returns a string, not an object. So this is equivalent to writing "a1_mc".myTxt_txt.text = bla, which of course isn't going to work. To access an object by a variable value in JavaScript, you have to use bracket notation on the object's parent object. So something like this[myBtns[k]].myText. etc...

 

To make a variable available throughout an entire timeline, assign it as a property of this. Note that this will point at window instead of the current timeline when accessed inside an event handler.

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