views:

187

answers:

2

I'm converting an old AS1 project to AS3 and am running into a little problem.

Previously, I was attaching random movieclips that are linked from the library with id's like movie1, movie2, movie3, etc. Here was my code:

var ranID = random(4)+1;
var mc = attachMovie("movie"+(ranID), "mc"+ranID, ranID);

In AS3, I've given each of these movieclips in the library a class name, so that I can do something like:

var mc = new movie1();

However, i want that to be a random mc... any ideas?

+1  A: 

Figured it out:

var mc = new (getDefinitionByName("movie"+Math.floor(Math.random()*4)) as Class);
addChild(mc);

Maybe there's a better solution out there, but this worked fine for me.

majman
A: 

I would do something like this:

private var movieList:Array = [ 
    movie1, 
    movie2, 
    movie3, 
    movie4 
    ];

public function getRandomMovie():MovieClip {
  return new movieList[Math.floor(Math.random()*movieList.length)];
}

/* later */
var mc:MovieClip = getRandomMovie();
addChild(mc);
davr