Skip to main content
Participant
December 1, 2007
Answered

Can a loaded SWF call a function that lives in the parent?

  • December 1, 2007
  • 3 replies
  • 620 views
I'm building a pretty simple Flash site in AS3. There is a main movie (main.swf) that simply loads different swfs via buttons on a main nav bar. The user clicks on a MC in "main.swf" and a function is called, loadMyContent("section1.swf"), is called and it animates in nicely. Other buttons use the same function, loading section2.swf, section3.swf, etc.

I've defined how "loadMyContent()" works in the the main movie's document class file. It's all working fine when I just need to load content from a user action from the buttons that live in "main.swf". I want to call that same "loadContent" function from within "section1.swf" and have "main.swf" run it's "loadContent" fuction, but I can't seem to figure out how to make a child call a function that lives in the parent.

Is there any way of having my child do this?

I have a suspicion I may have to define that "loadContent" somewhere else, but I'm a little stumped now. I'm not really familiar with design patterns yet, although I want to get an understanding of them sometime soon. Can anyone offer some help with my immediate need or suggest a route to a solution? Thanks.
This topic has been closed for replies.
Correct answer johnfmorton
kglad,

Thank you very much! That worked perfectly. My section1 FLA is now compiling it's SWF without complaint.

In case someone else is following this, the exact code I ended up using to cast "this.parent.parent" as a MovieClip is:

MovieClip(this.parent.parent).loadMyContent("section2.swf");

The discussion I think kglad is referencing is this discussion. If that's not it, just let me know. Again, kglad, thank you!

3 replies

kglad
Community Expert
Community Expert
December 3, 2007
you're welcome.

but no, that wasn't the thread where someone asked about creating a global reference to the main timeline. it was here:

http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=15&catid=665&threadid=1317823&highlight_key=y
kglad
Community Expert
Community Expert
December 2, 2007
cast this.parent.parent as a movieclip.

you can create a globally available reference to your root timeline and the stage using a mainTL class that i just posted in this forum in the past 1 or 2 days.
johnfmortonAuthorCorrect answer
Participant
December 2, 2007
kglad,

Thank you very much! That worked perfectly. My section1 FLA is now compiling it's SWF without complaint.

In case someone else is following this, the exact code I ended up using to cast "this.parent.parent" as a MovieClip is:

MovieClip(this.parent.parent).loadMyContent("section2.swf");

The discussion I think kglad is referencing is this discussion. If that's not it, just let me know. Again, kglad, thank you!
kglad
Community Expert
Community Expert
December 1, 2007
yes. the loading swf's main timeline is usually the parent.parent of the loaded swf's main timeline.
Participant
December 1, 2007
Thank you for the reply.

I think I'm targeting the main timeline of my "main.swf" using "this.parent.parent". (It seems correct when I trace that path from within my "section1.swf".) When I try to actually call that function though, my "section1.swf" won't compile correctly. It says I'm calling an undefined method. Here's the exact error:

quote:

1061: Call to a possibly undefined method loadMyContent through a reference with static type flash.display:DisplayObjectContainer.
Participant
December 2, 2007
Looking around the forum some more, I came across this post about root timeline access.

I think we're trying to solve a similar issue. How to make a root timeline globally accessible. I think this might be the path to the solution... still working on it though.