0

我试图让孩子阅读其父母的变量,反之亦然。父母阅读孩子的变量没有问题,但由于某种原因,孩子只能得到“未定义”作为答案......(而不是“456”)

父脚本

var mySwf
var masterVar=456
function startLoad() { 
    var myLoader:Loader = new Loader(); 
    var mRequest:URLRequest = new URLRequest("test1.swf"); 
    myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
    myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
    myLoader.load(mRequest); 
} 

function onCompleteHandler(loadEvent:Event) {
    mySwf=loadEvent.currentTarget.content
    addChild(mySwf);
    trace(mySwf.slaveVar)//123
}

function onProgressHandler(mProgress:ProgressEvent) {
}
startLoad()

Child(test1.swf)脚本

var slaveVar=123

trace(Object(parent))//[object Loader]
trace(Object(parent.parent))//[object Object]
trace(Object(parent.parent).masterVar)//undefined
trace(Object(this.parent.parent).masterVar)//undefined

parent.parent.parent 为 null 并且 MovieClip(parent.parent) 只吐出一个错误

我不知道出了什么问题......我错过了什么吗?

4

1 回答 1

3

试试这个:

在子 swf 中:

var theParent:Object;
addEventListener(Event.ADDED_TO_STAGE, onAdded);
function onAdded(e:Event):void
{
    theParent = this.parent as Object
    trace(theParent.masterVar);
    //will work after child swf has been added to the display list of the parent file.
}
于 2013-02-26T11:18:40.517 回答