3 Replies Latest reply on Feb 13, 2008 10:18 AM by C-Rock

    Passing variables between loaded swfs

    jonbradley Level 1
      I have a base swf file and then I'm loading various files in at various points using loadMovieNum when necessary and I'm struggling getting them all to read the same variables.

      So I have what I'll call movie1. Into that I'm loading movie2 and setting variables in there which I've set as _root.sCount (for example). All good so far. But then I need to load movie3 into movie2 and for it to read the variables I've set in movie2 but it won't.

      Is _root the wrong thing to use? Does that just look at the _root of the particular swf and not the overall root? If so what do I use, I've tried _global but no joy.

      Does that even make sense? Really quite urgent so if anyone can help it would be much appreciated.

      Cheers,

      Jon Bradley
        • 1. Re: Passing variables between loaded swfs
          Level 7
          Yes, _root is the wrong thing to use, and _global is never good practice. I
          don't use loadMovieNum - I'd use MovieClipLoader and it's loadClip method to
          load other swf's into target clips. You can then use something like
          this._parent to get to the timeline of the clip 'containing' the loaded
          clip.
          However, I think your best bet is to probably use a singleton class. That
          way you have your main movie instantiate and set variables within the
          singleton - then any other movies can easily create an instance (which will
          be the same instance since it's a singleton) and get the values you need.

          Simple example:

          class VariableHolder{
          private static var instance:VariableHolder;

          private var myA:Number;

          private function VariableHolder(){}

          public static function getInstance():VariableHolder{
          if(instance == undefined){
          instance = new VariableHolder();
          }
          return instance;
          }

          public function setVariableA(newA:Number){
          myA = newA;
          }

          public function getVariableA():Number{
          return myA;
          }

          }

          So, in your main movie you'd do:

          var myVariables = VariableHolder.getInstance();
          myVariables.setVariableA(50);

          And then in another movie you do:

          var myVariables = VariableHolder.getInstance();
          var myA = myVariables.getVariableA();
          //will set myA = 50;

          Because you're using a singleton - the second getInstance() call returns
          just the one class instance - held in the class' instance variable. You can
          have any/all movies instance the class, and only one instance is ever
          returned... Once you wrap your head around this it's a very efficient way to
          pass stuff around between various movies.


          --
          Dave -
          Head Developer
          http://www.blurredistinction.com
          Adobe Community Expert
          http://www.adobe.com/communities/experts/


          • 2. Re: Passing variables between loaded swfs
            jonbradley Level 1
            Thanks for that. Can't claim to understand it all yet but I'll have a proper read through it and work it through. Like you say it's a case of getting your head round things and once you have it becomes easier.

            Thanks for a very comprehensive reply!
            • 3. Re: Passing variables between loaded swfs
              C-Rock Level 2
              use loadMovieNum and use this "_level5.mymovieClip.gotoAndPlay("up");"


              _level --- will access the root of whatever level you load into and want the root of...