0

I have 5 different arrays for a bunch of excercises, like so:

    const oef1:Array = ["citroen","schoen","boek"];
    const oef2:Array = ["huis","muis","jas"];
    const oef3:Array = ["boek","koek","sok"];
    const oef4:Array = ["ei","bij","bot"];
    const oef5:Array = ["vier","mier","muur"];

Now I want to set the current game. I do this by copying the array, like so:

var curArr:Array;
var curExc:int = 1;
curArr = ("oef" + curExc) as Array;

I can't convince flash to accept the string ("oef"+curExc) as an Array. How do I do this?

I have searched Stack Overflow extensively but I think I simply don't know the correct lingo for what I'm looking for. It's the only possible reason I can think of why I can't find the answer here because I'm sure someone must have already tried this. Apologies if this is the case, please point me to the right question.

3 Answers 3

2

Use square brackets.

curArr = this["oef" + curExc];
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I don't understand exactly why your solution works though. I always thought you can only use this[bla] when referring to items added to the displaylist?
That's the notation for assembling a property name from other variables. It's also often used when referring to dynamic properties just to make clear that they are dynamic as opposed to writing them out verbatim.
2
const oef1:Array = ["citroen","schoen","boek"];
const oef2:Array = ["huis","muis","jas"];
const oef3:Array = ["boek","koek","sok"];
const oef4:Array = ["ei","bij","bot"];
const oef5:Array = ["vier","mier","muur"];

const oefArrays:Array = [oef1,oef2,oef3,oef4,oef5];


var curArr:Array;
var curExc:int = 0;
curArr = oefArrays[curExc];  

1 Comment

Thanks, this solution works too! I suppose now it's only a matter of personal preference which one to use. In that case I prefer the first solution, because I don't have to add yet another extra array.
0

You could make a two-dimensional array, like so:

wrapper => [
    0 => oef1,
    1 => oef2,
    2 => oef3,
    3 => oef4,
    4 => oef5,
]

And just retrieve wrapper[curExc].

Have a great day.

2 Comments

This looks like advanced array magic to me :) I have never worked with two-dimensional arrays before but I will certainly look into it, thanks!
It's the same thing that Sam suggested, only expressed in a different manner :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.