AS3:外部类中的访问函数位于外部swf

问题描述:

我想访问一个函数,在一个加载的swf中有外部类.. 我想避免必须将函数放在我的“Main”Doc类中外部SWF ,而是直接从类AS3:外部类中的访问函数位于外部swf

该访问功能是什么香港专业教育学院试图到目前为止,这是一个没有骰子:

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,并且没有默认值。 at com.util :: ClickArea/completeHandler()“

有一个解决方案。 ...先谢谢了!

在第二种情况下尝试这个办法:

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返回类。

+0

var myGreet:Class = event.target.applicationDomain.getDefinition(“com.view.Creative”)as Class;这解决了它!非常感谢@turbosqel – user992731

+0

没问题,您应该阅读一些关于ApplicationDomain的内容以了解它是如何工作的;) – turbosqel