Skip to main content
April 17, 2009
Answered

accessing clips from the library with dynamic name?

  • April 17, 2009
  • 1 reply
  • 574 views

trying to create a new MC on my stage using a dynamic name ala:

var jpn:Sprite  =  "jpn_andy";

var sample = new jpn();

addChild(sample);

Is there a way to do this? I tried a few variants on the above, but wasn't getting anywhere fast. Please help.

Bj

This topic has been closed for replies.
Correct answer Ned Murphy

Here's some code from a different point of view that may be what you're after:

  var dynamicClassName:String = "xmlString";
  var classRef:Class = getDefinitionByName(dynamicClassName) as Class;
  var img:MovieClip = new classRef();

It still requires having objects with Class IDs defined in the library... that's the only way to pull library content in.

1 reply

Ned Murphy
Legend
April 17, 2009

I'm not sure if this is what you are trying to do...

First you need to designate the object in the library as an item that can be loaded dynamically.

Right click on it in the library and select Linkage from the menu that appears. In the interface that appears, select Export for Actionscript. A Class name will automatically be assigned, which you can change as you like (lets just say you name it MyObject). This is the name you will use to call in the item from the library.

When you click OK to close that interface, it will come up with an indication saying it can't find the class so it will create one at complie time... click OK there to.  You're done preparing the object in the library.

To add the item to the stage, treat it like any other new object instance:

var newObject:MyObject = new MyObject();
newObject.x = .... etc...
this.addChild(newObject);

April 17, 2009

Thank you, but not what I need. I have a name that I am pulling from an XML file and I need to attach (aka:new) the library item with the corrisponding name.

what I am trying to avoid:

var _myItemName1 = new myItemName1();

var _myItemName2 = new myItemName2();

var _myItemName3 = new myItemName3();

var _myItemName4 = new myItemName4();

var myName = _myItemName4

function addItem(myName) {

     addChild(myName);

}

hope this explanation helps get the right answer.

Ned Murphy
Legend
April 17, 2009

The followup explanation may or may not get better results.  It's not clear to me what you are trying to do, so you may have to explain each detail of it.  My best shot at interpretting it still leads to the same conclusion, but it can probably avoid what you were trying to avoid (not sure what that is though)...

var myName = new myItemName4();