我试图让孩子读取其父母的变量,反之亦然。 父母在阅读孩子的变种时没有任何问题,但出于某种原因,孩子只能得到未定义的#34;作为答案...(而不是" 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)只发出错误
我不知道什么是错的...... 我错过了什么吗?
答案 0 :(得分:3)
试试这个:
在孩子们的孩子们:
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.
}