Reputation: 15
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?
Upvotes: 0
Views: 3339
Reputation: 6722
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.
}
Upvotes: 3