Archive for the ‘AS2’ Category
AS2 Pseudo-enumerations and constant collections
Prerequisites: Knowledge of basic Actionscript 2 OOP
Note: I am not assuming any kind of innovation here. I have been made aware that this a method adopted by Adobe for their Flex framework and AS3 best practises, and as such, i appear to have merely been independently clever for once ![]()
What are enumerations, and why bother?
Enumerations are a concept i’ve encountered a bunch in the past, but never really understood until recently. Usuallywhen spotted in the wild, they take this form:
enum AIStates{
idle,
chasing,
attacking,
searching,
patrolling
}
What does this tell you, at first glance? We are obviously creating some sort of data structure, an array of what looks like properties without values. From the name “enumeration”, we can assume (and correctly) that each of these values ARE assigned values. As an object structure, the above example would take this form:
AIStates= {
idle = 0;
chasing = 1;
attacking = 2;
searching = 3;
patrolling = 4;
}
Still, what good does this do us? At runtime these values are still just references to numbers, so there’s no REAL advantage, is there? As i was trying to wrap my head around this concept, i did pretty much exactly the opposite of what i should have done; i made an AS2 Enum class with functionality for adding and removing elements.
The fundamental flaw here is that enums are, by nature, static. They are never meant to change, as they describe constant states. You could translate the whole idea, loosely, to a collection of static constants. As such, my class was pointless. No enum should ever change at runtime.
Let’s reaffirm this important truth:
What is a constant? A value that never changes at runtime.
In a game application, a constant could be a player’s default walk speed, its run speed, how much damage a bullet of a certain type does.
It could be the sound a door makes when it opens, the speed at which it opens, how much damage it can take before breaking.
An enumeration, though a collection of constants, is also commonly a collection of string identifiers referring to numeric IDs. At runtime, a conditional checking a numeric value takes considerably less resources than a conditional comparing strings. In this way, for the sake of conditionals and identification, enumeration have an immediate advantage.
function updateAI(actor:Actor):Void{
switch(actor.state){
case 0:
//patrol corridors
break;
case 1:
//chase player
break;
case 2:
//search for player
break;
}
}
Now with an enumeration
function updateAI(actor:Actor):Void{
switch(actor.state){
case States.PATROL:
//patrol corridors
break;
case States.CHASE:
//chase player
break;
case States.SEARCH:
//search for player
break;
}
}
I assume, as a developer, you’re not missing out on the other great advantages here.
- Readability is greatly improved
- The actual values of the constants are externalized, meaning locating their values and altering them is much less of a hassle.
- The constant values are grouped and externalized, meaning they can be referred by multiple classes
The advantages are just too many to ignore. There is one more serious advantage however, entirely at development time: If you use FlashDevelop, Primalscript or any other third party Actionscript IDE with intellisense-style code completion, you will never have to remember the exact value of a constant ever again, nor will you ever have to deal with plain numeric values alone for the advantage of conditional simplicity.
Now for our actual AS2 implementation
Actionscript 2 has no inherent enumeration type, nor does AS3. My implementation is more of a pseudo-enum than an actual barebones enumeration in that it only truly cares about the fact that it’s a collection of static constants. As an AS “traditional” enumeration would force you to type in constant values regardless, there is literally no real advantage to limiting the structure to integer values.
Summing up, our implementation is an abstract class populated by public static variables.
class enums.EnemyStates{
private function Playerstates(Void){
//private constructor, abstract class
}
public static var IDLE:Number = 0;
public static var CHASING:Number = 1;
public static var ATTACKING:Number = 2;
public static var SEARCHING:Number = 3;
}
There you are. A collection of constants referrable by any other class.
A more complicated example, with enum cross references:
class enums.Characters{
private function Characters(Void){
}
public static var ANDREAS:Character= new Character("Andreas",24,enums.CharacterTypes.LEAD_VOODOO_ENGINEER);
public static var GARY:Character= new Character("Gary",enums.Defaults.DEFAULT_CHAR_AGE,enums.CharacterTypes.AWKWARD_SOCIALLY);
}
So we see, our constant collections can naturally hold static instances of any datatype. Commonly, we’d avoid any datatype that actually manifests itself in the application, such a MovieClip or Sound, leading us to a second fundamental truth:
Constants are set before their context has initialized, and do not change during the context’s execution
I strongly urge you to play around with this methodology in FlashDevelop. Since i began using it, i have no idea how i’ll ever go back to initializing constants in every class instance.
Further reading:
enums @ wikipedia
Leave a Comment