Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Community Beginner ,
Dec 29, 2016 Dec 29, 2016

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-exter...", 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!

461
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Dec 29, 2016 Dec 29, 2016

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

Translate
Community Beginner ,
Dec 29, 2016 Dec 29, 2016

kglad​ it is here!

Plz help me!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 29, 2016 Dec 29, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 29, 2016 Dec 29, 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);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 29, 2016 Dec 29, 2016

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

}

...

}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 30, 2016 Dec 30, 2016

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 30, 2016 Dec 30, 2016
LATEST

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

Screenshot - 12_30_2016 , 7_05_00 AM.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines