Search code examples
actionscript-3flash-cs6

Movieclip names


I'm having troubles with AS3 and addChild methods.

First, I create an object called "container". Inside container I create an empty object with an empty MovieClip from library called "holder". Then I create the Movieclips inside the container.holder But I cannot access to the MovieClips! Anyone knows why? Here is the code:

// Creating object
var container:Object {
   x: 30,
   y: 30
}

// Empty object
var eObject: MovieClip = new MovieClip();
container.holder = eObject;

// Creating Movieclips
var mc : MovieClip;
   for (var i : int = 0; i < 5; i++) {
      var mc: _myClip = new _myClip(); // _myClip is a MC from my library.
      mc.name = "myMc"+ i;
      mc.x = 10;
      container.holder.addChild(mc);
}

// Calling MovieClips
container.holder["myMc"+3].x = 40; // Nothing happens

Solution

  • You can reduce the complexity and needless usage of the name property by using an Array:

    var items:Array = [];
    
    for (var i:int = 0; i < 5; i++) {
        var mc:_myClip = new _myClip();
    
        container.holder.addChild(mc);
        items.push(mc);
    }
    
    items[2].x = 40;