简体   繁体   English

AS3-从Document类外部访问库项目

[英]AS3 - Accessing Library Items from outside the Document class

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: 例如,如果我在Flash中创建一个影片剪辑并将其导出为名称为Foo的Actionscript,则可以在文档类中执行此操作:

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. 如果在SomeOtherClass.as中使用与上面相同的代码,那么舞台上什么也没得到,显然是因为此类不了解库中的Foo对象。

I know I probably need to use appplicationDomain.getDefinition somehow. 我知道我可能需要以某种方式使用appplicationDomain.getDefinition。 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. TypeError:错误#1007:尝试在非构造函数上实例化。

Looks like an application domain problem. 看起来像一个应用程序域问题。 The loaded swf cannot access classes defined in the loader. 加载的swf无法访问加载器中定义的类。

You should give the loaded swf access to the loader swf library. 您应该授予加载的swf访问加载程序swf库的权限。 Try using LoaderContext . 尝试使用LoaderContext

Off the top of my head: 从我的头顶上:

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

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. 我认为(已经有一段时间了!)您需要编写一个从MovieClip或类似内容派生的代码类,并将其与库项目关联在其属性中。 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. 当然可以吠叫正确的树。

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. 我相信对于库符号,符号名称开头不应包含程序包名称。

I find these answers somewhat over-cooked, because all I've ever done to use a Library Item from any class is to import it like any standard import. 我发现这些答案有些过分,因为使用任何类的Library Item所做的全部工作就是将其像任何标准导入一样进行导入。 Surely that's why we select Export for Actionscript. 当然,这就是为什么我们选择“导出为动作脚本”的原因。

For example: 例如:

package 
{
   import foo;                     // Library MovieClip
   import flash.display.MovieClip; // Must import MovieClip to Extend it.
   .......

   public class something extends MovieClip
   {
     private var _foo:foo;   // Create a local reference of the imported Library Item.
     ......

    public function something():void
    {
     _foo = new foo(); // Create a new instance of the imported Library Item.
     .........
    }
   }
  }  

I find this works each and every time, can't say whether or not it's the right way. 我发现这项功能每一次都能奏效,不能说是否正确。 but it works for me. 但这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM