Fernando Comet
Fernando Comet

Reputation: 36

actionscript 3: how to access to elements of an array created in a loop dynamically

In the library of the .fla file I have a square exported as Class "cuad" on frame 1

I want to create an Array with 100 squares so as to move them later

So I do like this:

for (var i:uint = 0; i<100;i++)
{
var cuad_mc = new cuad();
addChild(cuad_mc);                      
myArray.push("cuad_mc");
trace(myArray[i]);
}

I have a runtime error

Upvotes: 0

Views: 72

Answers (1)

user2655904
user2655904

Reputation:

The error you experience is

Error #1069: Did not find alpha propiety in the String and there is not any value predetermined

The problem comes from your line

myArray.push("cuad_mc");

What you are doing here is pushing a String Object into your Array, not the cuad Object you want. String Objects don't have Alpha values, or x values.

What you want to do is

myArray.push(cuad_mc);

cuad_mc (without the " quotation marks) is a reference to the object you just created.

This should solve your problem. I also recommend using Vectors instead of Array if you only need to store one type of Object. Like this:

var myArray:Vector<cuad> = new Vector<cuad>();
for(var i:int=0;i<100;i++){
    var cuad_mc:cuad = new cuad();
    addChild(cuad_mc);
    myArray.push(cuad_mc);
    trace(myArray[i]);
}

Vectors are just like Arrays, but they only allow one specific type, so that a situation like yours doesn't occur.

Upvotes: 3

Related Questions