两个SWF间共享事件,那末这两个SWF就可以通过共享事件通信,例子是改动网上的,例如:
public class Main extends Sprite
{
public var shared:EventDispatcher;
public function Main()
{
var loader:Loader = new Loader();
shared = loader.contentLoaderInfo.sharedEvents;
shared.addEventListener("fromChild", fromChild);
var url:String = "http://localhost:9080/lcds/Flex3007/Child.swf";
loader.load(new URLRequest(url));
addChild(loader);
}
private function fromChild(event:TextEvent):void {
trace(event.text); // Good day
var replyMessage:TextEvent = new TextEvent("fromParent");
replyMessage.text = "Same to you";
shared.dispatchEvent(replyMessage);
}
}
这个是使用contentLoaderInfo的属性sharedEvents, 该EventDispatcher可用于跨安全边界交换事件,从而达到通信的目地;
http://localhost:9080/lcds/Flex3007/Child.swf
public class Child extends Sprite
{
public function Child()
{
var shared:EventDispatcher = loaderInfo.sharedEvents;
shared.addEventListener("fromParent", fromParent);
var firstMessage:TextEvent = new TextEvent("fromChild");
firstMessage.text = "Good Day";
//child实例时,抛出事件信息“Good Day”
shared.dispatchEvent(firstMessage);
}
public function fromParent(event:TextEvent):void {
trace(event.text); // Same to you
var label01:TextField=new TextField();
label01.text=event.text;
addChild(label01);
}
}
运行: