ほぼ実験のように3つの方法を考えました。
1)表示オブジェクトの階層をたどる方法
2)LocalConnectionを使う方法
3)共有オブジェクトを使う方法
1)
//Main.swfのメインのタイムライン
var a_array:Array=["メインの配列1","メインの配列2"];
var loader:Loader=new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onCompleteListener);
loader.load(new URLRequest("Sub.swf"));
function onCompleteListener(evt:Event):void {
//trace(loader.content)
addChild(loader);
}
//Sub.swfのメインのタイムライン
// btn:MovieClip、info_txt:TextFieldがある
addEventListener(Event.ADDED_TO_STAGE,onAddedListener);
function onAddedListener(evt:Event):void {
info_txt.text="追加された";
btn.addEventListener(MouseEvent.CLICK,onClickListener);
}
function onClickListener(evt:MouseEvent):void {
var maintimeline:DisplayObject=stage.getChildAt(0);
info_txt.text="クリック"+"\n"+MovieClip(maintimeline).a_array;
}
2)
//Main.swfのメインのタイムライン
var a_array:Array=["値1","値2"];
var loader:Loader=new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onCompleteListener);
loader.load(new URLRequest("Sub.swf"));
var lc:LocalConnection=new LocalConnection();
function onCompleteListener(evt:Event):void {
addChild(loader);
lc.send("lcconn","sendMes",a_array);
}
//Sub.swfのメインのタイムライン
var lc:LocalConnection=new LocalConnection()
var client:CustomClient=new CustomClient()
lc.client = client
lc.connect("lcconn");
addEventListener(Event.ADDED_TO_STAGE,onAddedListener);
function onAddedListener(evt:Event):void {
info_txt.text="追加された";
btn.addEventListener(MouseEvent.CLICK,onClickListener);
}
function onClickListener(evt:MouseEvent):void {
info_txt.text="クリック"+"\n"+client.getArray()
}
// CustomClientクラス
package {
public class CustomClient extends Object {
private var myArray:Array;
// これはよくない! 悪用される!
public function sendMes(arr:Array):void {
myArray=arr;
}
public function getArray():Array {
return myArray;
}
}
}
3)
//Main.swfのメインのタイムライン
var a_array:Array=["値1","値2"];
var so:SharedObject = SharedObject.getLocal("test","/");
so.data.arr = a_array;
so.flush();
var loader:Loader=new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onCompleteListener);
loader.load(new URLRequest("Sub.swf"));
function onCompleteListener(evt:Event):void {
addChild(loader);
}
//Sub.swfのメインのタイムライン
addEventListener(Event.ADDED_TO_STAGE,onAddedListener);
function onAddedListener(evt:Event):void {
info_txt.text="追加された";
btn.addEventListener(MouseEvent.CLICK,onClickListener);
}
function onClickListener(evt:MouseEvent):void {
var so:SharedObject = SharedObject.getLocal("test","/");
if (so.size == 0) {
// 共有オブジェクトは存在しない
} else {
info_txt.text="クリック"+"\n"+so.data.arr;
}
}
----------------
永井勝則:
himco.jp :