• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2660
  • Last Modified:

passing variable between parents and children in AS3

I am loading a clip by using a loader like this:

var url:String;
var request:URLRequest;
var Loader_1:Loader;

url = "home.swf";
request = new URLRequest(url);
Loader_1 = new Loader();
Loader_1.load(request);
Loader_1.name = "loaded_clip";
Loader_1.contentLoaderInfo.addEventListener(Event.COMPLETE, load_complete);

// the load complete function
// ----------------------------------
function load_complete(e:Event) {
      main_stage.addChild(Loader_1);
      removeEventListener(Event.COMPLETE, load_complete);
}

// end of code

Now I simply want to be able to pass a variable either to the child clip from the parent or grab a value from the parent in the child clip, but all of the usual ways that I have seen posted do not work. Is this even possible?

So far I have tried many different variations of:

          MovieClip(parent.parent).whatever = 10;

and nothing is working.

If I try to trace the parent of the child clip, the farthest I can go back is the loader itself. If I try to get the parent of the loader, I get a 'null reference' error. I've given the loaded clip a name of "loaded_clip" and added it as a child of "main_stage", but when I try to get the parent of "loaded_clip", I get the same null reference error.

How the heck do you pass variables when you can refernce the parent clip?

Thanks
0
section25
Asked:
section25
2 Solutions
 
moagriusCommented:
until it's in the display list, the Loader won't have a parent.  no matter how quick it appears to happen, there's a cycle where there is no parent, even if you do it like this:

addChild(new Loader());

even that ^ will have a null parent for a cycle.

the easiest way to get around that is to perform those operations in an addedToStage handler.

so in the swiff to be loaded:

function myFunc(event:Event):void{
  trace(MovieClip(parent.parent).whatever);
}
addEventListener("addedToStage",myFunc);
0
 
blue-genieCommented:
Hi there.
I don't know how big this project is that you're working on, but I would seriously recommend you move away from how you're working if it's a big project with many sub swfs that you're loading etc.
the initial learning curve and setup of creating a document class and other objects and util classes may be hectic, but once you get that - it's like riding a bicycle. it also makes it so much easier to send and receive values back and forth using custom listeners etc.
however if you're doing a quick simple app you can access your variables as follows.

just a quick thing, theres 2 different loader events that you should take note of.

1. Event.COMPLETE - which you're using is best used for preloaders etc, literally it says okay the bytes are loaded. however if you want to access the variables in the file you just loaded you need the INIT event

2. Event.INIT - this means the stuff is initialised - you cannot access something until it's initialised other wise you get the good old null or undefined errors

so. having send that have a look at the code snippet - see the //blue genie bits to note changes i'm made or added to your code.

shout if it doesn't make sense



//IN THE FILE THAT IS LOADING THE SWF

//blue-genie start
var whatEver:Number = 100; //your main timeline variable that you want to send to the child.
//blue-genie end
var url:String;
var request:URLRequest;
var Loader_1:Loader;
url="home.swf";
request=new URLRequest(url);
Loader_1 = new Loader();
Loader_1.load(request);
Loader_1.name="loaded_clip";
Loader_1.contentLoaderInfo.addEventListener(Event.COMPLETE, load_complete);
//blue-genie start
//added the INIT listener as mentioned - haven't done the clean up i.e remove so do that 
Loader_1.contentLoaderInfo.addEventListener(Event.INIT, onInit);
function onInit(e:Event) {
 MovieClip(Loader_1.content).myVariable = whatEver;//set the value of the child variable to its own.
 MovieClip(Loader_1.content).updateTextField();//call a function in the child just to display it
}

//blue-genie end

// the load complete function
// ----------------------------------
function load_complete(e:Event) {
	main_stage.addChild(Loader_1);
	removeEventListener(Event.COMPLETE, load_complete);
}

// end of code


//*****************************************
//IN THE FILE THAT IS BEING LOADED
//all i've done is created its own variable and a textfield to display that value
//then i created a function to update the textfield value
//now the parent file updates the value of the variable and calls the update function .

var myVariable:Number = 0;

function updateTextField():void {
	varTest.text = myVariable+"";
}

updateTextField();

Open in new window

0
 
72lionsCommented:
I totally agree with blue-genie .
You must add the loaders content on the main_stage, add a pointer to that content, and then use the pointer.

Now, you will have to change the following:
var openedMovieClip:MovieClip;

function load_complete(e:Event) {
      openedMovieClip = Loader_1.content;
      main_stage.addChild(openedMovieClip);
      Loader_1.removeEventListener(Event.COMPLETE, load_complete);
}

openedMovieClip.myVariable = "test";
trace(openedMovieClip.myVariable);

//IN THE FILE YOU WILL LOAD
//ADD THE FOLLOWING LINE OF CODE
var myVariable:String= "";

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
blue-genieCommented:
ok to prevent confusion, either use my way as is, or follow 72lions, not both.
0
 
72lionsCommented:
Yes blue-genie is right. One of the two solutions. But I strongly advise you to start coding with Object Oriented Programming.

My solution will help you since you started working this way.
0
 
section25Author Commented:
OK, Thanks everyone for the help.

I came up through the ranks from Flash 5 through 6,8, CS3 and now CS4. I have always found it easy to use the procedural code of the Flash IDE for everything I need. I dabbled with the OOP code when CS3 came along, but had a hard time connecting the code to the timeline and other timely events, so I abondoned it. The IDE has given me everything I've need so far, but I would like to learn the OOP version of doing things as well.

What is the best way to get started with AS3 OOP? Buy a book? If so which book? Internet tutorials?, etc?

Thanks for the help.
0
 
blue-genieCommented:
i've only been doing AS3 "properly" the last 4 months or so.
I do have some understanding of OOP as I as forced to do some Java programming for a while.
I'd say start with basic OOP principles if you don't know them, i.e. class structures, public/private static etc - all that fun stuff.

then start with a simple little hello world app. and go from there.
there are some great books out there, but the net also has great free information.
you just need to take the first small step.

good luck.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now