tomaugerdotcom
asked on
Creating class-wide ("package") variables with AS 2.0
Let's say I have an AS2.0 class called myClip, and in my calling script I invoke multiple instances of myClip() objects. Is there any way, within the class itself, to keep track of how many myClip objects have been created?
Currently, I'm doing this in the calling script, by just creating a variable which I increment each time a new myClip is created. But that would cause problems if I destroy a clip and would want to then decrement that value.
It would be so much more convenient if I could create a variable that's "global" across all instances of the class, like the VB "shared" accessor. I believe AS3.0 allows "package" variables which are accessible across the whole package, is that right?
At any rate, is there any way to do this in AS2?
Tom
Currently, I'm doing this in the calling script, by just creating a variable which I increment each time a new myClip is created. But that would cause problems if I destroy a clip and would want to then decrement that value.
It would be so much more convenient if I could create a variable that's "global" across all instances of the class, like the VB "shared" accessor. I believe AS3.0 allows "package" variables which are accessible across the whole package, is that right?
At any rate, is there any way to do this in AS2?
Tom
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Good stuff. Thank you! Static vars work in AS2 just like in AS3, as it turns out, except there's no package definition, I guess.
Actually they do have packages in AS2, they're just not required the way they are in AS3. In AS3, if you don't put your classes in a package, an unnamed package is created at compile time to hold all unpackaged classes. Glad I could help.
eD
eD
class myclass{
//static variable declaration
private static var _instances = 0;
//public access to variable
public function instances(){
return _instances;
}
//then in your class constructor, increment this variable
public function myclass () {
//increments here for each new object
_instances++;
}
}
//access the value like so
trace(myclass.instances);
//should work