Jump to content
Search Community

How can a variable contained in a child swf be loaded into the parent using LoaderMax

Xristi test
Moderator Tag

Recommended Posts

Hi,

 

I can only get a null from the code referenced above... and, as you wrote, that means that I have the child swf running on its own. However, the onComplete function that I wrote into the code (in the SWFLoader in the parent) indicates that the child is loaded. What must I do differently? On a personal note, I am a 4th year PhD student in Ed. Tech. writing an intelligent tutoring system to teach story problem solution. I have been working on this system for over two years learning as3 as I go - your help is greatly appreciated as my target date of graduation is fall of 2015.

Link to comment
Share on other sites

I tested the files again and when I export the parent fla, the child swf gets loaded and reads the myName variable from the parent.

 

And yes, if you run child.swf on its own, you will get a null value.

 

I'm not sure what more I can do in addition to providing a fully working example. 

Link to comment
Share on other sites

Hi,

 

I meant that when I run the parent I got the null in the child, both from the first trace and the second trace in the if statement. I work in CS4, but I ran your code in a trial version of CC2014.1 exactly as presented.

 

One last try - I have attached a zip file with my CS4 code (which fails to load the child, but gives no "null" messages either). Can you look at it and see why it is failing.

 

One thing that puzzles me about you response above, though, is your reference to a variable myName. The code that I got from you uses the function doStuff. Maybe we are not looking at the same code?child to parent info passing.zip

Link to comment
Share on other sites

Yes, I don't think you are using the files I linked to above named callFunctionInParentSwfFromChild_CS5.zip (found in this post)

 

I looked at your files. Had to fix them a bit as your Parent.fla had a document class of "parents" but the ActionScript file was called ParentMain.as. Same problem in Child.fla, it wasn't using ChildMain.as

 

Another problem was that you need to make sure the loaded swf is actually added to the display list before you start looking for parents. This is described here: http://www.emanueleferonato.com/2009/12/03/understand-added_to_stage-event/

 

 

I made some changes to ChildMain.as 

You should be able to replace your code with the following and have it work

package
{
import flash.display.MovieClip;
import flash.events.Event;


public class ChildMain extends MovieClip
{


public function ChildMain()
{
addEventListener(Event.ADDED_TO_STAGE, init);
}


function init(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, init);


if(this.parent.parent)
{
trace("init " + this.parent.parent.parent.parent.parent);
//call a function in the parent fla
MovieClip(this.parent.parent.parent.parent).doStuff();
}
else
{
trace("it's null");
}
}


}
}

Oh, and there is a change in ParentMain.as. 

You were adding holder_mc to the display list before the ParentMain() function even ran

 

package
{
import flash.display.MovieClip;
import flash.display.*;
import com.greensock.loading.*;
import com.greensock.events.LoaderEvent;
import com.greensock.loading.display.*;


public class ParentMain extends MovieClip
{
var holder_mc:MovieClip = new MovieClip();




public function ParentMain()
{
addChild(holder_mc);
   var swf:SWFLoader = new SWFLoader("Child.swf", {container:holder_mc, onComplete:loadComplete});
swf.load();
}


function loadComplete(event:LoaderEvent):void
{
trace("load is complete");
}


function doStuff()
{
trace("child told me it was loaded"); 
}
}
}
Link to comment
Share on other sites

Hi,

 

Finally got it to work. Thank you very much again, for your patience and expertise. I did have to juggle the number of parent(s) in ChildMain. The trace statement in init() took 4 and the MovieClip took 3 to get rid of the runtime errors variously regarding null objects and illegal conversions. This is how it looks in my code now:

 

if(this.parent.parent)

{
    trace("it's here");
    trace("init " + this.parent.parent.parent.parent);
    //call a function in the parent fla
    MovieClip(this.parent.parent.parent).doStuff();

}

 

As you can tell, I added parent (s), moving up the display list, until I received no RT errors - I see that you used 5 and 4. Why would it be different on your computer or is there something I am missing in my knowledge of such things?

Link to comment
Share on other sites

I don't think your computer should have any different results than mine.

I just traced the 4 parents  using this code in ChildMain.as

 

trace(this.parent)
trace(this.parent.parent)
trace(this.parent.parent.parent)
trace(this.parent.parent.parent.parent);
MovieClip(this.parent.parent.parent.parent).doStuff();

this is what I got

[object Loader]
[object ContentDisplay]
[object MovieClip]
[object ParentMain]

My guess is that in your parent fla you are not placing the loaded swf in a holder_mc via the SWFLoader's container property.

Link to comment
Share on other sites

Thank you for the response,

 

I get:

 

1[object ContentDisplay]
2[object MovieClip]
3[object ParentMain]
4[object Stage]

 

so one index off from yours. I checked my code and couldn't find any differences - but there must be something. I will look at it again, soon.

 

Again, thank you so much! I learned more about as3 than just how to read variables/functions between parent and child-

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...