Search code examples
actionscript-3flashflashdevelopflash-cs6

A class needs to be able to see a variable in Main AS3


I'm working with Flash CS6 and FlashDevelop and all the similar questions I could find deal with classes accessing stuff in other classes, but not in Main so here it is: My main initializes the level i made in Flash like so:

public class Main extends MovieClip 
{
private var MazeNr1:Level = new Level();

public function Main():void 
{
    if (stage) init();
    else addEventListener(Event.ADDED_TO_STAGE, init);
}

private function init(e:Event = null):void 
{
    removeEventListener(Event.ADDED_TO_STAGE, init);
    addChild(MazeNr1);
}
}

And I need my instantiated level variable (MazeNr1) to be seen by other classes in my project so that I can use its parameters (like width and height which are not the same thing as stage.parameters). Thank you.


Solution

  • Assuming Main is your document class, you could do this a few different ways.

    1. As mentioned in another answer, make your variable public. Then access it like so:

      Main(root).MazeNr1
      
    2. Make it a static variable

      public static var MazeNr1:Level;
      
      public function Main():void 
      {
          MaxeNr1 = new Level();
          //rest of your constructor code
      }
      

      Then you can access it in any scope simply by doing this:

      Main.MazeNr1