views:

1374

answers:

4

How do you access Library items from classes other than the document class?

For example, if I create a movie clip in Flash and Export it for Actionscript with the name Foo, I can do this in the document class:

var f = new Foo();
this.addChild(f);

And it appears on the stage, as it should. But I need to be able to create other instances of this object from other classes. If I use the same code above in SomeOtherClass.as, I get nothing on the stage, obviously because this class doesn't know about the Foo object in the library.

I know I probably need to use appplicationDomain.getDefinition somehow. This doesn't work:

var a = new ApplicationDomain(ApplicationDomain.currentDomain);
var foo: Class = a.getDefinition ( "com.me.CustomClass" ) ;
var f = new foo( ) ;
addChild ( f ) ;

TypeError: Error #1007: Instantiation attempted on a non-constructor.

A: 

I don't ever remember this being a problem. I think (it's been a while!) that you need to write a code class derived from MovieClip or similar and associate with the library item somewhere in its properties. Of course once this is done, you can treat the library classes the same as any other class.

EDIT:

try this:

var foo:Class=loaderInstance.contentLoaderInfo.applicationDomain.getDefinition("MyClass");
var x:*=new foo();

Upon closer reading of your problem, I realize that this is code at the loader side, not the loadee side. Definitely barking up the right tree though.

spender
Yes, your code works if called in the loader, but in that case I can access the library anyway without using applicationDomain.I'd like to know how to use that same type of call, but within the "loadee" (the secondary loaded swf).
A: 

Try something more like this:

var foo:Class = ApplicationDomain.currentDomain.getDefinition("SymbolName") as Class;

I believe the symbol name should not have a package name at the beginning, for a library symbol.

Jason S
ReferenceError: Error #1065: Variable CustomClass is not defined.I'm trying to access the library from a secondary swf that has been loaded. I need to get access to the parent swf's library.
A: 

Looks like an application domain problem. The loaded swf cannot access classes defined in the loader.

You should give the loaded swf access to the loader swf library. Try using LoaderContext.

Off the top of my head:

var loader:Loader = new Loader(); var ctx:LoaderContext = new LoaderContext(false,ApplicationDomain.current); loader.load(yourRequest,ctx);

Juan Pablo Califano
If I use your suggestion in the loader, what would the code be to create an instance of an object from the main library, called from some other class that is compiled with the child swf?
try this:var clazz:Class = loaderInfo.applicationDomain.getDefinition("ClassName") as Class;var instance:ClassName = new clazz();
Juan Pablo Califano
A: 

I wrote a blog entry explaining this very issue. You can see it here: http://mikedaross.tumblr.com/post/255348816/using-external-classes-with-applicationdomain

Mike Daross