as3 跨域加载swf资源,并使用其中的代码
as3的安全沙箱很严格,跨域加载swf资源或使用资源中的代码的时候会出现若干问题,例如通过原件的库链接名"role"创建对应的实例的时候会出现"role"未定义的错误
不管放置swf资源的的server上有没有crossdomain.xml文件, 只要这个swf资源有类似 Security.allowDomain("*"); 的权限, 那么别的域的swf就能得到相应的资源
如果你用Loader加载as3 swf 资源的时候应该用这样的方式写 load:
//
public function load(url:String):void{
urlStr = url;
var tempReq:URLRequest = new URLRequest(url);
if(loader != null){
removeListeners( loader );
}
loader = new Loader();
configureListeners(loader.contentLoaderInfo);
var context:LoaderContext = new LoaderContext();
context.applicationDomain = ApplicationDomain.currentDomain;
try{
loader.load(tempReq,context);
}catch(e:Error){
trace("error::PicLoader : "+e);
}
tempReq = null;
}
//
urlStr = url;
var tempReq:URLRequest = new URLRequest(url);
if(loader != null){
removeListeners( loader );
}
loader = new Loader();
configureListeners(loader.contentLoaderInfo);
var context:LoaderContext = new LoaderContext();
context.applicationDomain = ApplicationDomain.currentDomain;
try{
loader.load(tempReq,context);
}catch(e:Error){
trace("error::PicLoader : "+e);
}
tempReq = null;
}
//
通过 库链接名得到对应的Class的时候要使用如下方式:
//
public function getClassByName(className:String):Class {
try {
return loader.contentLoaderInfo.applicationDomain.getDefinition(className) as Class;
} catch (e:Error) {
throw new IllegalOperationError(className + " definition not found in "+urlStr);
}
return null;
}
//
try {
return loader.contentLoaderInfo.applicationDomain.getDefinition(className) as Class;
} catch (e:Error) {
throw new IllegalOperationError(className + " definition not found in "+urlStr);
}
return null;
}
//
这样处理的话就顺利的使用跨域资源了。