PDA

View Full Version : variable scope


vancechristopher
01-31-2007, 12:25 PM
I have a problem understanding flash
I have tried several times to understand movie chain variables, but cannot seem to grasp the Idea.
I have the book Actionscript (the Definitive Guide). And in it it's talking about variables.
It states that if you put a movie on the stage an then go to the movie timeline and insert a variable you can access this variable from the main timeline by using the dot notation like so...

//text on first frame of layer one main timeline
trace(_root.square.x)

I have made a square movie and made sure that the square movie is a named instance on the main stage by typing in "square" in the property title label, and then went into the movie square and put this actionscript on frame one level one of square movie.

var x;
x = 20;

now when I test the movie the output is undefined.
If I make the variable from the main timeline like this on frame one layer one of the main timeline.
_root.square.x = 20;
then

trace(_root.square.x)

it does come up as twenty in the output window.
If I cannot access a variable that is on a timeline of an imbeded movie I cannot program my movie properly.
Im not sure weather I should check the box for export for action script or not , but I think that this does not matter. I have tried several times an have allways come up with the undefined. Is there something I'm doing wrong. I need to be able to us the dot notation for all kinds of things. I have followed the book on page fourty three Scenario2, but it does not work, eventhough the book says it should. I understand the dot notation, but it doesn't seem to be working with the trace command.
any help would be gratefull.

Morg
01-31-2007, 12:47 PM
Hey vancechristopher

Welcome to the forums.

I am going to give you a nutshell description:

The stage (White block when you open Flash) is known as _root.
If you create a movieclip and put it on the stage (call it MC for now), then you can refer to it as _root.MC. because it is on the _root.

If you create a variable on the root (myVar = "hi"), you can reference it as _root.myVar

Now, if you put a textfield (myTxt) inside MC, you can refer to it as _root.MC.myTxt.

If you have some code inside MC and you want to reference a variable that you created on the _root, you can reference it as _root.myVar. Alternatively, you have created a variable inside MC. You can reference it as either _root.MC.var or var (If you are referencing from MC)

Does this make sense? :)