6 Replies Latest reply on Feb 23, 2009 1:02 PM by SiHoop

    Passing variables between swf files

    SiHoop Level 1
      How can I pass a variable from one swf to another? swf A contains login information about a user. swf B is loaded into swf A, but needs the login information. The variable is public in swf A, but I don't know how to access that information in swf B.
        • 1. Re: Passing variables between swf files
          flexstone
          The method that works for me is to create a Model in the application for login info and store user data there upon successful login.

          <mx:Model id="userInfo?>
          <info>
          <userName/>
          <userPriv/>
          </info>
          </mx:Model>

          Once the userModel is populated you can access it from any application child (component) using this reference

          In the children components create a variable for the app

          thisApp = mx.core.Application.application
          then in the children use
          var userName = thisApp.userModel.userName
          • 2. Re: Passing variables between swf files
            SiHoop Level 1
            Thanks, but I believe that your solution assumes that the variable being sent and received are in the same Flex application. However, I have to send my variable from one swf to another swf. If I try your solution I get an error on the receiving file because thisApp.userModel.userName does not exist.
            • 3. Re: Passing variables between swf files
              flexstone Level 1
              Okay I misunderstood, so your swf A is not the parent application (or parentDocument) ?

              If your SWFs are both children of an HTML parent then this gets very messy , not sure if there is an eloquent way to do this - its much easier when one SWF is the parent of another or....

              When both are children of the same parent SWF( Flex application) you could use the parent application to control communication between the two and "push" variables from one to another.
              • 4. Re: Passing variables between swf files
                SiHoop Level 1
                Right-- one file was created in Flash, the second in Flex.
                • 5. Re: Passing variables between swf files
                  flexstone Level 1
                  Okay if swf A is the parent application and it loads swf B you can use this method - assuming you have a variable in swfB "userName" and in the parent application ( swfA ) you have a userModel.userName.

                  It loads the child swfB and populates its variable "userName" in this sample.......

                  private function loadContent():void{

                  ldr = new Loader();
                  var urlReq:URLRequest = new URLRequest("swfB.swf");
                  ldr.load(urlReq);
                  ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
                  ldr.contentLoaderInfo.addEventListener(Event.INIT, onLoaded);
                  swfA.addChild(ldr);
                  }

                  private function onLoaded(event:Event=null):void {
                  var flexSWF:MovieClip = event.target.content as MovieClip;
                  flexSWF.addEventListener(Event.ENTER_FRAME, checkContents);
                  }

                  private function checkContents(event:Event):void {
                  var mc:MovieClip = event.target as MovieClip;
                  if(mc.document) {
                  mc.removeEventListener(Event.ENTER_FRAME, checkContents);
                  var swfChild = mc.document
                  swfChild.parameters['userName'] = userModel.userName
                  }
                  }
                  • 6. Re: Passing variables between swf files
                    SiHoop Level 1
                    I looked at your code and there are a few things I don't understand. First, in the loadContent function you have a listener with a handler named 'loaded'. Should it be onLoaded? I believe the idea is to trigger the onLoaded function at the start and end of the loading process.
                    Second, I don't understand what happens in checkContents. What is 'mc.document'? I think this is the swf that was loaded into swfA, i.e. swfB. Does checkContents send the variable 'userName' to swfB as follows: Login.userName="101"?
                    Finally, what do I do in swfB to capture the variable userName? I've tried to capture the variable with Application.application.parameters.loginID, but it does not show up! I don't have a userModel.userName in swfA, but thought I could just hard code the value '101' in its place.
                    Thanks for your help. Below is the code I have in my Flash file (it's in the timeline, not in a class; does that matter?)

                    loadContent()
                    function loadContent():void{
                    var ldr = new Loader();
                    var urlReq:URLRequest = new URLRequest("Login.swf");
                    ldr.load(urlReq);
                    ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded)//loaded);
                    ldr.contentLoaderInfo.addEventListener(Event.INIT, onLoaded);
                    this.addChild(ldr);
                    }
                    function onLoaded(event:Event=null):void {
                    var flexSWF:MovieClip = event.target.content as MovieClip;
                    flexSWF.addEventListener(Event.ENTER_FRAME, checkContents);
                    }
                    function checkContents(event:Event):void {
                    var mc:MovieClip = event.target as MovieClip;
                    if(mc.document) {
                    trace("ending:"+mc.document)
                    mc.removeEventListener(Event.ENTER_FRAME, checkContents);
                    var swfChild = mc.document
                    swfChild.parameters['userName'] = "101"
                    }
                    }