views:

159

answers:

4

Hi.

In AS2 was easy to change a value from a variable that was in other timelines...

Now i cant do the old way!

Basically what i need is to change a value from a variable that is in the main timeline, from other timeline.

Example:

main timeline

var totalA:int = 0;

other timeline, after adding the eventListener to the function:

parent.parent.totalA++;

so..it doesnt work..Can anybody explain me why? Thanks

A: 

Well...i solved it.

Created a MovieClip with the path:

var A_mc:MovieClip = MovieClip(this.parent.parent);

Then i referenced it:

A_mc.totalA++;
dutraveller
be very careful with this.parent it has some strange features that adobe build into it for performance.
Fire Crow
A: 

A better solution would be to use a globals class. you reference it from all your classes and can set variables in it that are accessible everywhere.

take a look at http://www.uza.lt/codex/as3-global-object/

Global Object is a Singleton that lets you store dynamic variables in a globally accessible location within your AS3 application. This enables developers to accomplish things like self registering visual components, global events and event listeners.

Josh
A: 

It all depends on how you would like to do things, but a Singleton class for any global variables you may want to maintain is probably the best course. If you are not familiar with the singleton design pattern, merely stick to casting it as

//Example - Would move the parent movie clip 5 pixels to the right.
//You must cast it because there is no way for the compiler to know what the asset.
MovieClip(parent).x = 5;

Brian Hodge
blog.hodgedev.com

Brian Hodge
A: 

I would also add that if you're interested in OO practices I would not access that variable directly.

You can setup a function in the Singleton mentioned by Josh above. It helps to encapsulate that variable inside the function. You should name your function something meaningful, for example:

function addToTotal (int amount) :void

This way all the function for figuring out the total is encapulated within the Singleton and the function. If somehow the way you need to total changes later or if you need to change the varialbe, the change is limited to the function.

milesmeow