0

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 Answer 1

3

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.
}
Sign up to request clarification or add additional context in comments.

2 Comments

sadly, i only get the following error: "ReferenceError: Error #1069: Property masterVar not found on flash.display.Loader and there is no default value. at test1_fla::MainTimeline/frame1()" did i implement it wrong ?_?
ah thank you very much, didn't know i have to put it in an eventlistener! works now perfectly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.