今天要做一个Loading的swf文件,加载主文件,但要加载完成后给主文件赋值,
在网上查了一下,:
载入swf后,如何控制它?
在as3中,
swf A载入
swf B。用loader类载入的。但发现载入后,尽管在A中
trace(loader.content)明显能够指向B(因为会输出[Object B]这样的信息),但却无法访问其属性和方法,不知道怎么才能让A控制B啊?
var L:Loader=new Loader();
var UQ:URLRequest=new URLRequest("B.swf");
L.contentLoaderInfo.addEventListener(Event.INIT,onLoadInit);
L.load(UQ);
function onLoadInit(e:Event) {
var LC:Object=L.content
trace(LC.width);
trace(LC.height);
trace(describeType(LC))
LC.width=10;
}
addChild(L);
var UQ:URLRequest=new URLRequest("B.swf");
L.contentLoaderInfo.addEventListener(Event.INIT,onLoadInit);
L.load(UQ);
function onLoadInit(e:Event) {
var LC:Object=L.content
trace(LC.width);
trace(LC.height);
trace(describeType(LC))
LC.width=10;
}
addChild(L);
斑竹,我不是这个意思。
content是有很多属性可以读取。但是我需要的是,swf B中,有ducment class中定义的属性和方法,需要读取。但读取不了。
这个得通过LoaderContext,ApplicationDomain类来解决,域的指向问题
复制内容到剪贴板
代码:
B.swf中的B类//
package{
import flash.display.Sprite
public class B extends Sprite{
public var className:String="[class B]"
public function func():void{
trace("func()")
}
}
}
复制内容到剪贴板
代码:
LoadInClass类来进行读取B.swf///
package {
import flash.display.Sprite;
import flash.events.*
import flash.display.Loader
import flash.net.URLRequest
import flash.system.LoaderContext;
import flash.system.ApplicationDomain;
public class LoadInClass extends Sprite
{
private var L:Loader
private var UQ:URLRequest
public function LoadInClass()
{
L=new Loader();
UQ=new URLRequest("B.swf");
var context:LoaderContext = new LoaderContext();
context.applicationDomain = ApplicationDomain.currentDomain;
L.contentLoaderInfo.addEventListener(Event.INIT,onLoadInit);
L.load(UQ,context);
addChild(L);
}
private function onLoadInit(e:Event):void{
var classB:Class=L.contentLoaderInfo.applicationDomain.getDefinition("B") as Class
var b:Object=new classB()
trace(b.className)
b.func()
}
}
}
还有是也可用LocalConnection,不过不推荐!
// receivingLC is in http://www.domain.com/receiving.swf
receivingLC.allowDomain('www.anotherdomain.com');
receivingLC.connect('myConnection');
// sendingLC is in http://www.anotherdomain.com/sending.swf
sendingLC.send('www.domain.com:myConnection', 'myMethod');