Skip to main content
Known Participant
October 28, 2009
Question

global vars in as3

  • October 28, 2009
  • 2 replies
  • 741 views

Hi all,

I'm sure this is easy, but I can't seem to find out how to do this. I need to create a global var in AS3 so that I can create the var on the main timeline and change it within another movie clip. Thanks for the help in advance.

This topic has been closed for replies.

2 replies

October 29, 2009

If you design your program correctly there is rarely a need for global variables. That said, the easiest thing is to just make the variable a public member of the document class. Then any objects in the program can access this variable using "parent.myVar", etc. I generally like to pass the main DisplayObject in as a parameter of any instantiated class. That way I don't have to use "parent.parent" or any confusing syntax. You could also use "this.root" but I try to avoid that at all costs.

Inspiring
October 28, 2009

AAccessing main timeline is possible from anu clip in the stage with this: MovieClip( this.root ). This method cann't be used  in classes not added to stage not added to stage or not extedning Sprite. A real global is as3  is a class with static member var

class Globals

{

public static var myVar: uint;

}

myVar can be accessed from anywhere like this Globals.myVar

wwscoperAuthor
Known Participant
October 29, 2009

thanks.