03-10-2011 09:55 PM
MyApp.as
.
.
.
// make a MyClass class public var api:MyClass = new MyClass();
// make a MyOtherClass class
public var object:MyOtherClass = new MyOtherClass();
// make a MyThird class
public var object:MyThird = new MyThird();
.
.
.
MyOtherClass.as:
myButtonWasClicked(e:TypeOfEvent){
//run the method after the event!
MyThird.method();
}
MyThird.as is just a library i put to gether... it does nothing but process strings and things.
My question is how can i access an object (the instance of MyThird, called object in the 1st decleration ) inside of a DIFFERENT class like i am doing in MyOtherClass.
the idea is that there are seperate classes to my project (which are mainly UI elements) that all need access to this library. i'd like to only have to make one instance of the library in the MAIN class rather than instances in EACH class.
thanks!
Solved! Go to Solution.
03-10-2011 10:12 PM
Hi,
If I've got it right, you are trying to make your class behave like a static class, or in other words, a class that has only one single instance during your application life-cycle.
Although ActionScript doesn't have really a static class, you can get the same behavior using static methods and proprieties in a normal class. Let's say you have:
package
{
public class MyStaticClass
{
public static const MYVALUE:int = 100;
public static function MyStaticFunction(): void
{
trace("you called that");
}
}
}
Then you could call its methods and proprieties from any other class, like this:
MyStaticClass.MyStaticFunction(); trace (MyStaticClass.MYVALUE);
I hope it helps.
03-11-2011 07:52 AM
If this is what your are trying to do, you can have a static instance of the class (also known as a singleton class). This avoid static functions that can make some coding a little more difficult. Like everything, what ever gets the job done and is easy to maintain.
If you just need access of one class from another, you can just encapsulate the class in the other class. In some instances, you can pass a class as a reference to another class. If you need to pass events from one class (ui element) to another, you can have a centralized singleton class as an event dispatcher that allows event passing and global state information that your application may need. It allows for a loosely coupled application and avoids the need to pass class definitions around which can become a mess.
03-20-2011 06:01 PM
Sorry for the delay. I've been VERY busy.
making my class static did work. thanks!
I just want to point out that the class i needed to make static live inside of a different package, so i cant just do
myStaticClass.Function(variable)
i have to do
myCustomPackage.mySpecificStaticClass.manipulate(this, variale)
thanks all!
03-20-2011 06:24 PM
hey dtater,
if you import the class into your .as file, you should be able to do it the short hand way. do the following at the top of your .as file:
import myCustomPackage.mySpecificStaticClass;
from then on you should be able to do stuff like this:
mySpecificStaticClass.manipulate(this, variale)
hope that helps out. good luck!
03-20-2011 10:05 PM
Doh.

The More you know!
03-21-2011 12:30 AM
03-21-2011 12:36 AM
Thats exactly what this class is
.
i am taking the AS3 JSON library, moddifying it to better suit my needs, and making a whole series of functions designed to request and manipulate the json data. this way i can query a particular JSON api and process the results in a a few function calls.
See my signature for the details of the application... (as soon as it's done!)