2 Replies Latest reply on Jul 3, 2009 1:25 AM by Anthony Artmann

    Getting Variables from MovieClips

    Anthony Artmann

      I have built a MovieClip called TestMovieClip.

       

      The first frame includes the actionscript:

       

      var myValue:Number=42;

       

      function getMyValue():Number{
          return myValue;
      }

       

      The first frame of the main timeline includes the  actionscript:

       

      var testMovieClip:TestMovieClip = new TestMovieClip();

       

      addChild(testMovieClip);

       

      trace(testMovieClip.getMyValue());

       

      The movieclip is added but trace(testMovieClip.getMyValue()); returns NaN.


      I was expecting to get 42.  What am I doing wrong here?

        • 1. Re: Getting Variables from MovieClips
          kglad Adobe Community Professional & MVP

          you're not waiting until the first frame of testMovieClip has rendered.  ie, the first frame of the main movieclip executes code and then renders followed by testMovieClip's first frame executing code and then testMovieClip's first frame renders.

          1 person found this helpful
          • 2. Re: Getting Variables from MovieClips
            Anthony Artmann Level 1

            Thankyou kglad.  I get it now!

             

            I have changed the TestMovieClip actionscript to include an initialization function:

             

            var myValue:Number;

             

            function init():void{
                myValue=42;
            }

             

            function getMyValue():Number{
                return myValue;
            }

             

            The initialization function is called from the main timeline:

             

            var testMovieClip:TestMovieClip = new TestMovieClip();

             

            MovieClip(root).testMovieClip.init();

             

            addChild(testMovieClip);

             

            trace(MovieClip(root).testMovieClip.getMyValue());

             

            Is this the correct approach?  Or is there a more elegant solution?