Jump to content
Search Community

Loading variables from parent to child swf w/LoaderMax

Xristi test
Moderator Tag

Recommended Posts

Is there any way to use LoaderMax and possibly rawContent to provide variable values for the the child swf that are contained in the parent swf? I have a document class (MainCA) for the parent swf that contains the variables. When I use LoaderMax and the rawContent property I am given the document class of the parent as an object. Using dot notation for the specific variable produces an error "Error #1069: Property testString not found on MainCA and there is no default value".This is my first post and my first experience with LoaderMax, although I have used TweenLite for several years. I work in AS3. Thank you for any help that can be afforded.

Link to comment
Share on other sites

Hi and welcome to the GreenSock forums.

 

If you want the parent to assign a value to variable on the child it is fairly straight-forward, especially if you go through the SWFLoader that loaded the child like

someSWFLoader.rawContent.customVariable = "someValue";

If you want the child swf to search up the display list chain for values on the parent, it is a little more work. 

The number of parents a loaded swf has depends largely on whether or not the SWFLoader had a container property or how deeply it is nested in the parent.

 

Here is a demo that loads a child swf and the child swf calls a function on the parent and also grabs a variable value from the parent.

//only if the child has been loaded into a parent
if(this.parent.parent){
  //all parents along path: child Stage / Content Display / container / parent Stage
  MovieClip(this.parent.parent.parent.parent).doStuff();
  parentName_txt.text = "parent myName = " + MovieClip(this.parent.parent.parent.parent).myName;
}

see attachement

callFunctionInParentSwfFromChild_CS5.zip

Link to comment
Share on other sites

Thank you, Carl for your prompt reply. Below is the test code I have written. I get this error msg when the first line in the function completeHandler is not commented out.

 

1119: Access of possibly undefined property rawContent through a reference with static type com.greensock.loading:LoaderMax.

 

As the code is now, the swf child is showing and the trace statement in the completeHandler is written. Is there something I need to do in the parent document file first?

 

import flash.display.*;
import com.greensock.*;
import com.greensock.easing.*

import com.greensock.loading.*;
import com.greensock.events.LoaderEvent;
import com.greensock.loading.display.*;

var queue:LoaderMax = new LoaderMax();
queue.append( new SWFLoader("checkExpressionTrial.swf", {estimatedBytes:3000, container:this, x:250, y:100, autoPlay:false, onComplete:completeHandler}));

queue.load();

function completeHandler(event:LoaderEvent):void
{
    //queue.rawContent.testString = "someValue";
    //trace("rawContent: " + queue.rawContent.testString); //Correct - event.target refers to the SWFLoader
    trace("it's loaded");
}

Link to comment
Share on other sites

It appears queue is your LoaderMax. You want the rawContent of the SWFLoafer that fired the complete event.

 

As the comments suggest. event.target is the SWFLoader so try.

 

event.target.rawContent.testString

 

If that doesn't work please post a zip of your files. Much easier to troubleshoot that way.

Link to comment
Share on other sites

Hi,

 

Thank you very much for your patience. Here is the code for the parent fla (SwfLoadTrial):

 

import flash.display.*;
import com.greensock.*;
import com.greensock.easing.*

import com.greensock.loading.*;
import com.greensock.events.LoaderEvent;
import com.greensock.loading.display.*;


var queue:LoaderMax = new LoaderMax();
queue.append( new SWFLoader("checkExpressionTrial.swf", {estimatedBytes:3000, container:this, x:250, y:100, autoPlay:false, onComplete:completeHandler}));

queue.load();

function completeHandler(event:LoaderEvent):void
{
    event.target.rawContent.testString = "it's here";
    trace("rawContent: " + event.target.rawContent.testString); //Correct - event.target refers to the SWFLoader
    trace("it's loaded");
}

 

Here is the code for the document file (MainCA) for the child swf:

 

package
{
    import flash.display.MovieClip;
    
    public class MainCA extends MovieClip
    {
        private static var rootInstance:MainCA = null;
        
        var  myCA2;
        var myCARF;
        var testString:String;
        
        var matchingParentheses:Boolean;
        
        //public function Main() class constructor
        public function MainCA()
        {
            rootInstance = this;
            
            myCA2 = new CheckAnswer2;
            myCARF = new C_aRelationalFunctions;
            
            myCARF.setProbLevel(0);
            myCARF.setProbNumber(0);
            myCARF.setProbFunction(0);
            numericalAnswer_txt.text = String(myCARF.returnFunction(inputExpression_txt.text));
            
            showTestString();
        }
        
        function showTestString():void
        {
            testString_txt.text = testString;
        }
    
        //function to get root instance of Main
        public static function getInstance():MainCA
        {
            return rootInstance;
        }
    }
}

 

the error I get now is: Error #1056: Cannot create property testString on MainCA.

 

Sorry, how do I send the .zip file?

Link to comment
Share on other sites

you can attach a zip using the "more reply options" button.

 

Please remove all code and assets from your demo files that are not necessary to replicate the problem.

For instance, I do not need to see any code like this:

 

myCARF.setProbLevel(0);
            myCARF.setProbNumber(0);
            myCARF.setProbFunction(0);
            numericalAnswer_txt.text = String(myCARF.returnFunction(inputExpression_txt.text));

 

Thanks

Link to comment
Share on other sites

OK - removed all extraneous code. There are two fla files (parent and child) with associated as. doc files (Main and MainChild). I am getting the rawContent trace value on the LoaderMax load of the child swf, but I don't understand how to read it into the child file. The zip of all files is attached.SWFTrialTwo.zip

Link to comment
Share on other sites

Thanks for the demo. Very helpful.

 

Yeah, it seems that your child SWF was trying to do things with local variables BEFORE the parent could assign them values.

 

In MainChild.as I set a separate function to do the heavy lifting.

 

package
{
import flash.display.*;
import flash.events.MouseEvent;


  public class MainChild extends MovieClip
    {
        var enteredNum:Number;
var addedNum:Number = 18;
var resultNum:Number;
      
  public function MainChild()
        {
trace("MainChild()");
}


//this function gets called from the parent swf
public function setNumber(num:Number):void {
enteredNum = num;
resultNum = addedNum + enteredNum;
input_txt.text = String(resultNum);
}
}
}

in Main.as we talk to the child with

 

function completeHandler(event:LoaderEvent):void
{
event.target.rawContent.setNumber(int(typedNum_txt.text));


trace("it's loaded");
}

files attached

 

SWFTrialTwo-GreenSock.zip

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...