0

Is there? Like Let's say I need 5 variables in ActionScript 3.0 and would like to name them as follows:

var myVar_1 = "things";
var myVar_2 = "things";
var myVar_3 = "things";
var myVar_4 = "things";
var myVar_5 = "things";

But instead of having to type them 1 by 1, would it work in a loop? I can't seem to make it work and would really love some help/advice on this matter.

2 Answers 2

3

Yes, you can create a dynamic property name using [ ] array access:

var variables:Object = {};

for(var i:int = 0; i < 5; i++){
    variables["myVar" + i] = "value " + i;
}

trace(variables.myVar3); // "value 3"

The variables object in this case could be replaced by any dynamic object, including MovieClips.

However, in most cases to store data by index it usually makes more sense to use an array. Example:

var variables:Array = [];
for(var i:int = 0; i < 5; i++){
    variables.push("value " + i);
}

trace(variables[3]); // "value 3"
Sign up to request clarification or add additional context in comments.

Comments

1

You should use Vector.<String>, Array, Object or Dictionary for that:

var variables:Vector.<String> = new <String>[];
for(var i:int = 0; i<5; i++)
{
    variables[i] = "things";
}

Comments

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.