【问题标题】:AS3: Access function in external class located in external swfAS3:位于外部 swf 中的外部类中的访问函数
【发布时间】:2011-12-13 09:04:50
【问题描述】:

我正在尝试访问已加载的具有外部类的 swf 中的函数.. 我想避免将函数放在外部 swf 中的“主要”Doc 类中 而是直接从类中访问函数

这是我迄今为止尝试过的,而且是没有骰子的:

 private function startLoad(){
        var loader:Loader = new Loader();
        var req:URLRequest = new URLRequest("one.swf");
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
        loader.load(req);

    }
 private function onLoadComplete(e:Event):void{
        var ClassDefinition:Class = e.target.applicationDomain.getDefinition("com.view.Creative") as Class;
        var butn:MovieClip = new ClassDefinition(0,0);////0,0 is x and y cordinates I have in the "com.view.Creative" class

        this.addChild(butn);
        butn.setVar("one");////"setVar" is the function in my external swf with the class "com.view.Creative"
    }

这是com.view.Creative.as中的函数

  public var storedVar:String

  public function setVar(str:String):void{
               this.storedVar = str;
               trace(storedVar)
    }

//返回“ReferenceError:错误 #1069:com.view.Creative 上找不到属性 setVar,并且没有默认值。”

这是我采取的另一种方法,但没有成功

  private function startLoad(){
        var appDomain:ApplicationDomain = new ApplicationDomain();
        var context:LoaderContext = new LoaderContext(false, appDomain);
        var loader:Loader = new Loader();
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler); 
        loader.load(new URLRequest("one.swf"), context);


    }

  private function completeHandler(event:Event):void { 
        var myGreet:Class = ApplicationDomain.currentDomain.getDefinition("com.view.Creative") as Class;                
        var app : MovieClip = new myGreet(0,0)
        addChild(app);
        app.setVar("one");////set var is the function in my external swf with the class "com.view.Creative" I am trying to access
        //event.target.content.setVar("one");///this works if I am placing my "setVar" function on my "Main" Doc Class which I am trying to avoid///

    }

这是com.view.Creative.as中的函数

   public var storedVar:String

   public function setVar(str:String):void{
               this.storedVar = str;
               trace(storedVar)
    }

//返回“ReferenceError: 错误 #1069: 在 com.view.Creative 上找不到属性 setVar 并且没有默认值。 在 com.util::ClickArea/completeHandler()"

必须有一个解决方案......在此先感谢!

【问题讨论】:

    标签: actionscript-3 oop class dynamic


    【解决方案1】:

    在第二种情况下试试这个:

     var myGreet:Class = event.target.applicationDomain.getDefinition("com.view.Creative") as Class;  
    

    event.target 是你的 contentLoaderInfo 。

    如果您将 swf 加载到新的 ApplicationDomain 中,则无法在 currentDomain 中查找定义。

    在第一种情况下,也许您已经在此别名下拥有类 Creative 但具有不同的参数,当您将 swf 加载到 currentDomain 时,class :"com.view.Creative" 不会覆盖现有的,而是从主 swf 返回类。

    【讨论】:

    • var myGreet:Class = event.target.applicationDomain.getDefinition("com.view.Creative") as Class;那解决了!!!!非常感谢@turbosqel
    • 没问题,您应该阅读一些有关 ApplicationDomain 的内容以了解它是如何工作的;)
    猜你喜欢
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    相关资源
    最近更新 更多