Juris Zebnickis
Juris Zebnickis

Reputation: 35

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.

Upvotes: 0

Views: 70

Answers (2)

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

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
    

Upvotes: 0

invisible squirrel
invisible squirrel

Reputation: 3008

Make the variable 'public':

public var MazeNr1:Level = new Level();

Then you can access it from outside the class.

Upvotes: 1

Related Questions