6 Replies Latest reply on Dec 30, 2016 7:05 AM by kglad

    How to pass variables from .fla file to .as file in as3

    maziarser Level 1

      I have a .fla file names test.fla and I have this variable in it:

       

       

          var my_var;

       

       

          stage.addEventListener(MouseEvent.CLICK, onLoaded);

       

       

          function onLoaded(e:Event):void

           {

         my_var = "myVariable";

         //trace(my_var);

          }

       

       

      I have a .as file called Main.as.

       

       

      I want to pass **my_var** from test.fla to the Main.as.

       

       

      I will really appreciate, if you can help me in this matter!

       

       

      It is noticeable that I have used the method mentioned in "http://stackoverflow.com/questions/9603049/actionscript-3-pass-a-variable-from-the-main-fl a-to-external-as-file", but it does not work for me!!!

       

       

      I wrote in my Main.as:

       

       

          package

          {

           public class Main extends Sprite

        {

            public function Main()

            {

          trace(root.my_var);

            }

           }

          }

       

       

       

      Thanks in advance!

        • 2. Re: How to pass variables from .fla file to .as file in as3
          kglad Adobe Community Professional & MVP

          make Main your document class and have it extend the MovieClip class.

          1 person found this helpful
          • 3. Re: How to pass variables from .fla file to .as file in as3
            maziarser Level 1

            kglad Main is already document class and I extend to MovieClip class.

             

            the code is like this:

            test.fls:

             

                import Main;

                var my_var;

                stage.addEventListener(MouseEvent.CLICK, onLoaded);

                function onLoaded(e:Event):void

                 {

                   my_var = "Maziar";

                   //trace(my_var);

                 }

             

             

            Main.as

             

            package

                {

                  import flash.display.Sprite;

                  import flash.geom.Point;

                  import flash.events.MouseEvent;

                  import flash.utils.Timer;

                  import flash.events.TimerEvent;

                  import flash.events.Event;

                  import flash.display.MovieClip;

             

             

                  public class Main extends MovieCLip

                 {

                   public function Main()

                  {

                    if (stage)

                    {

                      init();

                    }

                    else

                   {

                    addEventListener(Event.ADDED_TO_STAGE, init);

                  }

             

             

                  addEventListener(Event.ENTER_FRAME, waitForMyVar);

                 }

             

             

                 private function waitForMyVar(e:Event):void

                 {

                   if (my_var != null)

                   {

                     trace(my_var);

                     removeEventListener(Event.ENTER_FRAME, waitForMyVar);

                    }

             

                  }

             

             

               private function init(e:Event = null):void

              {

                removeEventListener(Event.ADDED_TO_STAGE, init);

              }

                 ...

            }

            }

             

             

            I still get the errors "error: 1120: Access of undefined property my_var".

             

            the error is in line: if (my_var != null) and line : trace(my_var);

            • 4. Re: How to pass variables from .fla file to .as file in as3
              kglad Adobe Community Professional & MVP

              that couldn't have been your document class or you would have seen an error.

               

              anyway, use:

               

              maziarser wrote:

               

              kglad Main is already document class and I extend to MovieClip class.

               

              the code is like this:

              test.fls:

               

              import Main;

              var my_var;

              stage.addEventListener(MouseEvent.CLICK, onLoaded);

              function onLoaded(e:Event):void

              {

              my_var = "Maziar";

              dispatchEvent(new Event('varReadyE'));

              }

               

               

              Main.as

               

              package

              {

              import flash.display.Sprite;

              import flash.geom.Point;

              import flash.events.MouseEvent;

              import flash.utils.Timer;

              import flash.events.TimerEvent;

              import flash.events.Event;

              import flash.display.MovieClip;

               

               

              public class Main extends MovieCLip

              {

              public function Main()

              {

              if (stage)

                      {

                        init();

                      }

                      else

                     {

                      addEventListener(Event.ADDED_TO_STAGE, init);

                    }

              this.addEventListener('varReadyE',waitForMyVar);

              }

               

               

               

              }

               

               

              private function waitForMyVar(e:Event):void

              {

              trace(my_var);

              }

               

               

              private function init(e:Event = null):void

              {

              removeEventListener(Event.ADDED_TO_STAGE, init);

              }

              ...

              }

              }

               

               

              • 5. Re: How to pass variables from .fla file to .as file in as3
                maziarser Level 1

                Thanks again!

                But I have still this error:

                1120: Access of undefined property my_var.

                 

                In line:

                trace(my_var);

                 

                In Main.as

                 

                I have changed trace(my_var) to trace ("Hello") and it does not work also.

                • 6. Re: How to pass variables from .fla file to .as file in as3
                  kglad Adobe Community Professional & MVP

                  attach a screenshot of your properties panel that shows Main is your document class:

                   

                  Screenshot - 12_30_2016 , 7_05_00 AM.png

                  1 person found this helpful