0

I have a bunch of classes for MovieClips in my library, such as

ship_3000aa
ship_3000ab
ship_3001aa

and so on.

I need to access them like this:

var image_key:String = "3000aa";
var ship:MovieClip = new _root["ship_"+image_key]();

All of this is happens inside a class, and all MovieClips are exported to frame 2, which is where the class in which this happens is instantiated.

root is the base root. "root" without "" is not found. The property is null on _root. The property does not exists on _stage either.

Is there a correct way to instantiate a library MovieClip class using the array access operator from within a class?

Thanks.

1 Answer 1

3

Either you can instantiate an object by keeping the classes in an Array:

var classArray:Array = [ship_3000aa, ship_3000ab, ship_3001aa];
var ship:MovieClip = new classArray[2]();

or instantiate an object by the class name by using flash.utils.getDefinitionByName (probably more useful in your case):

var imageKey:String = "3000aa";
var ShipClass:Class = getDefinitionByName("ship_" + imageKey) as Class;
var ship:MovieClip = new ShipClass();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.