Wednesday, October 29, 2008

Calling Parent Variables and Functions from a Loaded SWF

Let's say I have one SWF (child.swf) that I want to load into another SWF (parent.swf), then I want to call a function that is in parent.swf from the child.swf. In AS2 you could just do _parent._parent as many times as needed to reach up the tree to get to the parent's top level. In AS3 you need to use this code below instead. Put it inside the child.swf. I have no idea what this line of code means, but is magic:
MovieClip(parent.parent).ParentFunction();
...then this sample code goes inside the parent.swf on its main timeline:
//this sample code loads the external SWF into the Parent SWF
var indexSwfRequest:URLRequest = new URLRequest("index.swf");
var indexSwfLoader:Loader = new Loader();
indexSwfLoader.load(indexSwfRequest);
addChild(indexSwfLoader);
//This is the parent function that can be called from the command that is inside the child.swf
function ParentFunction():void{
trace("It worked!");
}

13 comments:

Anonymous said...

Thanks so much for posting this solution! I got this page after a long search.

Anonymous said...

Nifty. I also found out, when testing the child movie, you can add this to prevent errors:
if(MovieClip(parent.parent)!=null){MovieClip(parent.parent).ParentFunction();}

Unknown said...

Code looks great. I'm trying to access a variable rather than a function and I'm doing something wrong apparently. How do I modify this code to make that happen?

Parent:
var randomHomeLoading:Boolean = false;

Child:
if(MovieClip(parent.parent).randomHomeLoading)
{
trace("Good Times.");
}

I get this: TypeError: Error #1009: Cannot access a property or method of a null object reference.

Thanks!

Matt Maxwell said...

What I do is trace parent.parent... intil i get [object MainTimeline]. My guess is you don't have the right number of .parent's. So try tracing: trace(MovieClip(parent.parent)), trace(MovieClip(parent.parent.parent)), etc until you get the [object MainTimeline] returned.

Anonymous said...

Thanks, Matt. Works like a charm, but any idea why this works but not the good ol' parent.parent.ParentFunction()?

Anonymous said...

Thank you so much!!! This worked for me!

tahiche said...

Hi:
Jon Cole, I had the same problem as you, I couldn´t access a var in the parent swf.

"Parent:
var randomHomeLoading:Boolean = false;

Child:
if(MovieClip(parent.parent).randomHomeLoading)
{
trace("Good Times.");
}

I get this: TypeError: Error #1009: Cannot access a property or method of a null object reference."

Try this:

var par=parent.parent
trace(par.randomHomeLoading)


I have no idea why, to me it looks exactly the same...

Anonymous said...

Help! I can't get this to work when using document classes for both SWFs.

In my parent SWF's document class (called ParentMovie) I have the following code ...

package
{
import flash.display.MovieClip;
import flash.net.URLRequest;
import flash.display.Loader;

public class ParentMovie extends MovieClip
{
private var myUrlRequest:URLRequest;
private var myLoader:Loader;

public function ParentMovie():void
{
trace("ParentMovie()");
myUrlRequest = new URLRequest("childMovie.swf");
myLoader = new Loader();
myLoader.load(myUrlRequest);
addChild(myLoader);
}

public function parentFunction():void
{
trace("It worked!");
}
}
}


And in the child SWf's document class (called ChildMovie) I have the following code ...

package
{
import flash.display.MovieClip;

public class ChildMovie extends MovieClip
{

public function ChildMovie():void
{
trace("ChildMovie()");
MovieClip(parent.parent).parentFunction();
}

}
}



You can download a ZIP file containing my test files from here ...

http://www.adrianparr.com/zips/as3_childMovie_calling_parentMovie.zip

Thanks in advance.

Anonymous said...

BTW: I get the following error message at runtime ...

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at ChildMovie()

tigerlil said...

hey thanks this was very helpful. I'm a teacher and should know this, but just hadn't yet tried to call a function from the loading movie clip.

I could even explain what it's doing (type coercion of the main timeline to make it a MovieClip since roots are not technically MovieClips)

Anonymous said...

Hmmm, doesn't work for me unfortunately. Maybe something's changed since it was posted.

Unknown said...

I have spent hours for finding a solution of variable passing.
Your code doesn't work too :(

TypeError: Error #1009: Cannot access a property or method of a null object reference.

Reflexion Graphic said...

I got an error message too. But there is an other solution : One way to handle this is to dispatch an event from the child swf, and have the parent swf listen for that event.