PDA

View Full Version : Cross-Class variable access


MondayHopscotch
10-11-2008, 07:52 AM
So i have a base class on my document, it contains a variable that I wish to access from another class.

How can I access these variables from this other class? I have thought to make a quick access function that just returns it, but I don't know how to call that function either.

The problem basically exists because the class that holds the variable is the base class for the document, so it is not within any object that i can make a quick reference to. How do i get around this?

rawmantick
10-11-2008, 07:55 AM
You can make reference to that object in your sub class, and make a setter for it. Just set the propertu from document class.

MondayHopscotch
10-11-2008, 07:56 AM
You can make reference to that object in your sub class, and make a setter for it. Just set the propertu from document class.

I'm still somewhat new to Actionscript. Could you post a quick piece of example code so I can see how to do what you are saying?

rawmantick
10-11-2008, 08:08 AM
public class SomeClass
{
public function set prop(arg:sometype):void { _prop = arg; }
...
}
And inside you doc class:

var some:SomeClass = new SomeClass();
some.prop = variableYouWasAskingToPassButDidntKnowHow;

rawmantick
10-11-2008, 08:08 AM
In most cases using globals is a key of bad program architecture.

MondayHopscotch
10-11-2008, 08:11 AM
So if I put this:
var some:SomeClass = new SomeClass();
some.prop = variableYouWasAskingToPassButDidntKnowHow;

would it create a new instance of that class? Because the variable i'm trying to get is set dynamically through my main class.

rawmantick
10-11-2008, 08:20 AM
Sory, but I haven't understood a thing. What class? What do you mean dynamically?

MondayHopscotch
10-11-2008, 08:36 AM
Sory, but I haven't understood a thing. What class? What do you mean dynamically?

Ok.

So. This "base class" (not sure of its actual name), the one you set in the properties of your document:

http://i101.photobucket.com/albums/m64/MondayHopscotch/baseclass.jpg

That class gathers information from the various movieclips that are on the stage once the frame is loaded. (This is what i mean by "dynamically", it means it is not hard coded -- it is set durring runtime)

One of my classes needs to pull one of the variables set in the base class after it scans over the frame.

I've tried to use root and parent (although the object that is trying to access the variable is not a child of the base class), but neither work -- they come up with syntax errors.

I need a way of calling "my main class".player (player being the variable that i need), but i cannot figure out how to access it.

rawmantick
10-11-2008, 08:57 AM
Document class is set to root movie clip, so you can cast it ot your base class and get info through getters.

Also you can create custom class with required setter and associate it with your movie clips. You dynamically get objects from the main timeline, cast them to that custom close and store in document class. Then you simple set the setters of those mc's to what you need.