Search code examples
arraysactionscript-3for-loopinstances

In Actionscript 3, how to create many instances of the same symbol?


Though coding, I must make many copies of the same MovieClip to be placed on the stage, each to be manipulated code-wise on its own

For instance, I have a MovieClip named MC and I wish to have 99 copies of ít loaded onto the stage, each in a different x coordinate. What should I do?

I think on doing this:

Step 1: in the library, turning MC into a class

Step 2: placing the following code in the scene's script

var MyArray:Array = new Array

for (var i:int = 0; i<99;i++)
{
var MCInstance:MC = new MC
MC Instance = MyArray[i]
MovieClip.(MyArray[i]).x = i*30
}

Would that make sense?


Solution

  • That's probably the right idea, your syntax is just a little off. Try this:

    var myArray:Array = [];
    
    for (var i:int = 0; i < 99;i++)
    {
        var mc:MC = new MC();
        myArray[i] = mc;
        mc.x = i * 30
    }
    

    AS3 style conventions: use lowerCamelCase for variable names, don't omit constructor parens even though they are optional, and create arrays using literals (source).