Skip to main content
Known Participant
December 29, 2016
Answered

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

  • December 29, 2016
  • 1 reply
  • 525 views

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-fla-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!

This topic has been closed for replies.
Correct answer kglad

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

1 reply

maziarserAuthor
Known Participant
December 29, 2016

kglad​ it is here!

Plz help me!

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
December 29, 2016

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

maziarserAuthor
Known Participant
December 30, 2016

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);