Keno
Keno

Reputation: 2098

Using a loop to assign labels won't work in Actionscript 3. TypeError: Error #1010

I've used this website for many things and found a lot of useful information that has helped me create a randomized quiz mostly. I'm trying to make the code as efficient as possible and that has led me to this error.

I have created an array which uses the .push function to store existing buttons on the stage into the array for future use. The code shown below sets the label of each button correctly.

_buttons[0].label = xmlData.difficulty1.questions[num2].op1.text();
_buttons[1].label = xmlData.difficulty1.questions[num2].op2.text();
_buttons[2].label = xmlData.difficulty1.questions[num2].op3.text();
_buttons[3].label = xmlData.difficulty1.questions[num2].op4.text();

So naturally I wanted to make this a little more efficient and put these in a loop. Based on the logic this SHOULD work but doesn't.

for (var i:Number = 0; i < 4; i++)
{
    _buttons[i].label = xmlData.difficulty1.questions[num2].op[i+1].text();
}

This code should simply increment the array counter of _buttons and set the label for each. I mean it's simply the first set of code in a for loop right? However when I run it I get the following error: TypeError: Error #1010: A term is undefined and has no properties.

Now I know for sure that the first set of code works as I have tested it repeatedly but as soon as I decide to put it into that for loop, it fails. Could anyone explain why? Perhaps it's a limitation of the language itself? Maybe I'm missing a command?

Upvotes: 0

Views: 36

Answers (1)

Barış Uşaklı
Barış Uşaklı

Reputation: 13532

Try :

for (var i:Number = 0; i < 4; i++)
{
    _buttons[i].label = xmlData.difficulty1.questions[num2]['op'+(i+1)].text();
}

.op[i+1] tries to access a property called 1 under a field called op instead of op1

Upvotes: 1

Related Questions