Skip to main content
Inspiring
April 17, 2008
Question

accessing variables

  • April 17, 2008
  • 10 replies
  • 863 views
I may be asking too much from a forum... I've been trying to educate myself about AS3 and Ive learned the core concepts of syntax, OOP and all that. But one thing that I just cannot find out how to do is to access or change a variable in my root/stage timeline from a deep movieclip within a movieclip within... I know I could just use the parent reference but what if it is imposible to know? In AS2 I just used the _root.(myVariable). I think I am missing a some sort of concept. Thanks in advance. Jake
This topic has been closed for replies.

10 replies

Known Participant
April 21, 2008
You can bank on the main timeline being at "0th" child of stage. The only time it wouldn't be, is if you manually removed the main timeline, or manually changed its depth with actionscript. If you aren't doing either of those then it will stay put.

and now, a really long answer:
I don't think someone would ever really tamper with the main timeline's display list membership because there is no reason to. The main timeline does well enough as the root, even though Stage is technically the true root of the display list. I would say the main purpose of the scene graph / display list is to give structure to the geometric transformations of display objects and to give structure to the handling of "interactive" events. Because of that someones only motivation for altering its place in the scene graph is because they either don't like working with a MovieClip as a parent object, which is really picky, or they somehow think that removing this single graph member will give them a performance increase, which is so neurotic i don't want to even think about it.

anyway yeah. its always at 0.



Inspiring
April 21, 2008
Is the Main Timeline ALWAYS the first child? If not, is there a simple way of locking it in place?
April 21, 2008
So, just to REALLy clarify this thing:
I understand that the Main Timeline is a child of stage... That`s why I can`t just go stage.variableName. And the child in the first position is the Main Timeline?
Thanks again!
April 21, 2008
Big thanks, you guys! I just posted the same question 5 minutes ago and only now read this post :P
Inspiring
April 21, 2008
Thanks a LOT! Very helpful.
Participating Frequently
April 17, 2008
Another way to utilize "global" variables that is a little more OOP-friendly, and less reliant on the presence of stage is to use a Singleton-type object to provide access to a set of variables from anywhere in the application. Take a look at this code sample to get started.
Inspiring
April 17, 2008
Another question. can I access these global variables (using the method previously posted) from imported classes? What would the proper technique be?
Thanks - Jake
Known Participant
April 17, 2008
yeah good one ... I think that if the imported class is an extension of Display Object, then yeah. If not, you'll have to pass in a reference to the stage. Ideally this would be through the constructor. lets just make a worst case scenario here:

package
{
public class Basic
{
private var global;

public function Basic(theStage:Stage)
{
global = theStage.getChildAt(0);
// ok so now everything in this class can use the global property to get at these global variables
}

public function method1()
{
trace(global.count);
++global.count;
trace(global.count);
}
}
}


Alright now lets say we're at some timeline somewhere. I just instatiate Basic and pass in the stage. Like this:

var bsc;
bsc = new Basic(this.stage); // the "this" is optional because it is implied i just have it shown for clarity
bsc.method1();


And that should do it. If Basic were to be an extension of say, the Sprite class, anywhere in the class definition you could say "this.stage.getChildAt(0)" and it will work. However if you were to reference ".stage" in a display object class definition before it was added to the display list, it might be "undefined." Hope thats what you were after.
Inspiring
April 17, 2008
Thanks
Inspiring
April 17, 2008
thanks for the reply. So, just to clarify, you are saying that using the code you posted, I can just replace variableName with any variable and I can change its value and reference it? I am pretty sure thats what your saying, but I'm slow/new to AS3.

-Jake
Known Participant
April 17, 2008
Yep. Then you can read it, and write to it, from anywhere.
Known Participant
April 17, 2008
So you need a global variable?

I know a sloppy way... Each node in the display list has a handle on the root object, Stage, through the property ".stage"

However, you need the main timeline which is a child of stage, and not Stage itself. So we'll use Stage.getChildAt(0) to get at the main timeline from Stage. Try this anywere in the display list: