3 Replies Latest reply on Jan 11, 2007 8:50 AM by nchristiansen

    Accessing instance variables from container

    nchristiansen
      I am new to Flash. I am a programmer, not a designer so this is my first foray into Actionscript.

      I am creating a slide show player in Flash. It retrieves an XML file containing other .swf files that should be played, the order in which they should be played, the length of time each one should show, and the text that should be displayed.

      For example:
      <presentation>
      <globals>
      <setting name="use_transitions" value="false"/>
      <setting name="use_delays" value="false"/>
      </globals>
      <slide src="slide5.swf" text="Slide 1" delay="10"/>
      <slide src="slide3.swf" text="Slide 2" delay="10"/>
      </presentation>

      I have everything working except for the loaded .swf file picking up on the text to display.

      I can set the Var: property on the dynamic text field to _root.dynamic_text and it will load what is in the variable dynamic_text just fine.

      However, the slide3.swf and slide5.swf file have default text in them that should who if there is no text specified.

      It seems that variables inside a loaded movie are not in scope.

      I load the movie using:

      loadMovie(slides[nIndex][0], "slideMovie");

      However if my instance name on my dynamic text field is "theText" this code does not work:
      this.slideMovie.theText.text = dynamic_text;

      How do I dereference the dynamic text field?

      Thanks for any help.
        • 1. Accessing instance variables from container
          nchristiansen Level 1
          I have a workaround.

          Set the Var to "dynamic_text"

          In the Actionscript for the embedded clip put:

          var dynamic_text = "default text";
          if (_root.dynamic_text)
          {
          this.dynamic_text = _root.dynamic_text;
          }

          This is not an ideal solution. Anyone else?
          • 2. Re: Accessing instance variables from container
            abeall Level 3
            I didn't really follow your set up.
            > this.slideMovie.theText.text = dynamic_text;
            Where is that code placed? Where is theText? Where is dynamic_text defined, or what is it? _txt usually signifies a TextField instance, but if that was the case then the statement would not do anything. Are you trying to access objects in the external slide SWF before it has been loaded and initialized?
            • 3. Accessing instance variables from container
              nchristiansen Level 1
              quote:

              Originally posted by: abeall
              I didn't really follow your set up.
              > this.slideMovie.theText.text = dynamic_text;
              Where is that code placed? Where is theText? Where is dynamic_text defined, or what is it? _txt usually signifies a TextField instance, but if that was the case then the statement would not do anything. Are you trying to access objects in the external slide SWF before it has been loaded and initialized?


              I'm sorry that I wasn't clear enough.

              Maybe this will help.

              ============== Begin Code ==============

              slide3.swf
              ========
              _root
              layer |------> Instance of Graphic Symbol
              -----------------------> Dynamic Text Object named 'theText'
              layer |------> Instance of Graphic Symbol
              -----------------------> JPEG Graphic Background
              layer |------> Actionscript Layer


              slide_show engine.swf
              ==================
              _root
              layer |------> Movie Clip Object named 'slideMovie'
              layer |------> Actionscript Layer

              Actionscript code snippet:
              /* This becomes a two dimentional array of slides.
              * slides[index][0] is the .swf file to load.
              * slides[index][1] is the transistion to load. (Not currently used.)
              * slides[index][2] is the delay to wait (in seconds) until the next slide shows.
              * slides[index][3] is the customized text to show the loaded slide. If blank show the default text.
              */
              var slides = new Array();
              var dynamic_text;
              var nIndex = 0;
              var main:Object = new Object();

              main.switch_slides = function()
              {
              if (nInterval != -1)
              {
              clearInterval(nInterval);
              }

              if (nIndex >= slides.length)
              {
              return;
              }

              _root.dynamic_text = slides[nIndex][3];

              loadMovie(slides[nIndex][0], "slideMovie");

              this.slideMovie.theText.text = _root.dynamic_text;

              nInterval = setInterval(main, "switch_slides", (slides[nIndex++][2] * 1000));
              };

              // Code to load and parse the configuration file deleted for brevity.

              ============== End Code ==============


              The slides are made by our designer. Most of which animate the background image and then fade in the text.

              The text object usually does not exist in the first key frame. So my workaround is probably the only way this can be done.

              The workaround needs to go in every slide's actionscript layer on the first key frame.

              The only real problem I have with it is that I need to put the default text in the actionscript. Putting a Var reference in the dynamic text will remove the text there if the variable is empty.

              However trying to dynamically create the var reference does not seem to work. For example:

              In the slide3.swf file if I have the following code on the actionscript layer, the var reference in the dynamic text does not get pulled in properly:
              if (_root.dynamic_text)
              {
              this.dynamic_text = _root.dynamic_text;
              }

              I need to put the var reference at the top with the default text. For example:

              var dynamic_text = "default text";
              if (_root.dynamic_text)
              {
              this.dynamic_text = _root.dynamic_text;
              }