views:

404

answers:

3

Hello,

i'm porting an old AS2 project to AS3, And have encounter a problem. I've tried a few different things but had no success.

in AS2 when dynamically attaching a MC from the library i would sometimes use an array. the array would hold linkage reference's, like so;

var mc:String = state_ar[currentState];
this.container.attachMovie(mc,mc,this.getNextHighestDepth());
targetMC = this.container[mc];

How would i do this in AS3?

A: 

I'm not quite sure if this is what you're asking but I'll give it a shot.

//New array to store Movie Clips references    
var myArray:Array = new Array();
//Create a movie clip
var movieClip:MovieClip = new MovieClip();
//Add it to the array for reference
myArray.push(movieClip);
//Put it on the stage
this.addChild(movieClip);

You can still use arrays to store references to an object and you can reference them directly.


In as3 the MovieClips in the library will have a class name, so if you have a MC in the library called MyMovie you would add it in as3 with:

var myMovie:MyMovie = new MyMovie();
addChild(myMovie);
TripWired
sorry,i didn't explain myself very well. the array was used to store (what were called in AS2) linkages.for example;i have 3 different movieClips in my library, each with there own different linkage id. i create an array; state_ar = ['link_1','link_2','link_3']; then reference an index from the array; var linkageID:String = state_ar[0], then attach the movie;attachMovie(linkageID,linkageID, this.getNextHighestDepth()).
Cam
As3 is object oriented so you create and add movieclips by instancing them see my edited code. Hope that helps.
TripWired
thanks for your response TripWired, but the question refers to something else.
Cam
+2  A: 

You would do that using getDefinition() or getDefinitionByName()

Check out this answer for code, your situation sounds quite similar.

Note: The only difference is you might be using MovieClips, not images, so you won't need the 0,0 arguments in the constructor.

George Profenza
A: 

Answer;

var mc:String = state_ar[currentState];
var classReference:Class = getDefinitionByName(mc) as Class;
var tempMC:Object = new classReference();
this.container.addChild(tempMC)

Thanks George

Cam