import fl.controls.Button;
var childSWF:MovieClip;
var Parentvar:String = "parent variable";
//Parent TextField(上方, 準備置入Child Variable)
var ParentTF:TextField = new TextField();
ParentTF.y = 60;
addChild(ParentTF);
//Button
var btn:Button = new Button();
btn.move(20,20);
btn.label = "Passing Variables";
btn.addEventListener(MouseEvent.CLICK, clickBtn);
addChild(btn);
//swf載入
var loader:Loader = new Loader();
var urlrequest:URLRequest=new URLRequest("Child.swf");
loader.load(urlrequest);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, LoadComplete);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
//swf載入成功時, 顯示子swf在父swf上(調低子swf,以避免疊在一起擋到按鈕)
function LoadComplete(event:Event):void{
childSWF = event.target.content;
childSWF.y = 80;
addChild(childSWF);
}
//偵錯
function ioErrorHandler(event:IOErrorEvent):void {
ParentTF.text = "ioErrorHandler: " + event;
}
//按鈕後, 父子swf互換變數
function clickBtn(e:MouseEvent):void{
//呼叫父swf Variable, 傳到子swf TextField
if (childSWF != null){
childSWF.SetParentValue(Parentvar);
}
//呼叫子swf Variable, 傳到父swf TextField
if (childSWF != null){
ParentTF.text = childSWF.Childvar;
}
}