0

In my library I have a bunch of classes named tip1, tip2, tip3, tip4...and so on. Is it possible to create one instance of each on the stage using a for loop? I tried this but it didn't seem to work.

var tips:int = 12;
for(var i:int = 1; i<=tips; i++){
    var tipName:String = "tip"+i

    var tip:MovieClip = new tipName();
    tip.name = "tip" + i
    tip.x = stage.width;
    tip.y = 0;
    addChild(tip);
}

Any help would be appreciated. Thanks!

user1518911
  • 41
  • 1
  • 4

2 Answers2

3

You were missing the "getDefinitionByName" part.

// Up top
import flash.utils.getDefinitionByName;

// Down below
var tips:int = 12;
for (var i:int = 1; i < tips; ++i ) {
  var myClass:Class = getDefinitionByName('tip' + i) as Class;
  var tip:Object = new myClass();
  tip.name = "tip" + i; 

....

}
weltraumpirat
  • 22,544
  • 5
  • 40
  • 54
James Tomasino
  • 3,520
  • 1
  • 20
  • 38
0

Instead of

var tip:MovieClip = new tipName();

Try (written from memory)

var clazz:Class = getDefinitionByName(tipName) as Class;
var tip:MovieClip = new clazz();

Also, you generally want to use stage.stageWidth instead of stage.width, since the latter will return the stage bounding box width (which might not be the same as the area the swf file covers).

Jonatan Hedborg
  • 4,382
  • 21
  • 30