Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing variables from parent flash into child?

Status
Not open for further replies.

shedlord2

Technical User
May 20, 2006
36
GB
Hi, wondering if anyone here can help.

In brief, my question is, how can I pass a variable from a parent flash file to a child flash file which it calls?

- More detailed version:

We have a parent flash file which imports various child flash files based on instructions in an XML file. It's probably easier to see an example - - the text above each page, and the 'enlarge page' lightbox links are all in seperate flash files (one per double page) which are brought in, depending on the pages you are looking at, based on the instructions in an XML file which looks like this...

<page ani_swf="pages/text1.swf">pages/white.jpg</page>
<page ani_swf="pages/text1.swf">pages/0001.jpg</page>

<page ani_swf="pages/text2.swf">pages/0002.jpg</page>
<page ani_swf="pages/text2.swf">pages/0003.jpg</page>

etc, where the swf is the header flash file containing the text and one or two lightbox links, and the jpg files are the page images that appear.

We had to repeat the imported child flash file because of a glitch when you go backwards through the book otherwise.

This system has been fine for us up until now but recently we've had to look at making it a dynamic system, so the content of the book can be varied depending on a variable being passed into the parent file via FlashVars...

<param name="FlashVars" value="zone=1" />
<embed src="newbook3.swf" FlashVars="zone=1" ....etc

This makes the book load an xml file called "1"). This is fine, except we also need to vary the 'enlarge page' target images to match.

The 'enlarge page' links are currently hard coded into each child flash file, like this...

on (release){
getURL("javascript:SWFDelegate('imagename.jpg','Example');");
}

- What we've tried so far:

1. Trying to pass the variables in querystrings from the XML
eg.

<page ani_swf="pages/text2.swf?var1=image1&var2=image2">pages/0002.jpg</page>
<page ani_swf="pages/text2.swf?var1=image1&var2=image2">pages/0003.jpg</page>

with the child flash setting it's action like this...

on (release){
var image1 = var1 + ".jpg"
getURL("javascript:SWFDelegate(image1,'Example');");
}

I'm not sure if the syntax here is correct. We have to add the ".jpg" in here because, as far as I understand it, you can't pass "." in a querystring.

2. Passing the variables for the child into the parent

I read somewhere that variables passed to a parent are automatically available to a child. Not sure if this is true, but it didn't work for me when I tried it like...

<param name="FlashVars" value="zone=1?var1=image1&var2=image2" />
<embed src="newbook3.swf" FlashVars="zone=1?var1=image1&var2=image2" ....etc

Combined with...

on (release){
var image1 = var1 + ".jpg"
getURL("javascript:SWFDelegate(image1,'Example');");
}

...in the child button action.

Sorry if this is a bit confusing. It definitely confuses me!

Is there something really obvious we're missing here?

thanks
 
To keep it simple, can anyone explain the way(s) to pass a variable from a parent flash to a child. Ignore the detail above!
 
If the child is a MovieClip, give it a public property, then from the parent assign a value to that property. If you do it with a getter and a setter, you can automatically react to the assignment.
Code:
//Child MovieClip
public var myProp:Number;

//Parent Display Object
myChildMovie.myProp = 10;

Here is the same thing using a getter and setter.
Code:
//Child MovieClip
private var _myProp:Number;
public function set myProp(value:Number):void{
  _myProp = value;
  //react to the assignment
  trace("myProp was just given the value " + value);
}

public function get myProp():Number{
  return _myProp;
}

"Great spirits have always encountered violent opposition from mediocre minds." -Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top