as3 access variable from parent swf

谁说我不能喝 提交于 2019-12-08 12:00:24

问题


I'm trying to let the child read its parent's vars and vice versa. The parent has no problems reading the child's vars, but for some reason the child gets only "undefined" as an answer...(instead of the "456")

Parent script

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) script

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 is null and MovieClip(parent.parent) only spits out an error

I have no clue what's wrong... am I missing something?


回答1:


Try this:

In child 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.
}


来源:https://stackoverflow.com/questions/15086796/as3-access-variable-from-parent-swf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!