In learning AS3, one thing people find particularly difficult is the management of objects, both visual and not. Again, I’m no expert, but after spending some time with a few design pattern books, adding in alot of conversation, I came up with a few simple ideas and code segments to achieve some necessary goals.
Name Everything
The first goal is to easily access any object – either created dynamically or on the stage. Always Naming these objects becomes very important. For instance, if I add a shape to the stage dynamically, I also want to be able to easily access it and remove it if needed.
var myShape:Shape = new Shape(); myShape.name = "myShape"; addChild(myShape);
Since the object has a proper name, I can access it simply with something like myShape.alpha = 0.5;. If I were to not give it a name, it would be named something like instance435 instead – a generated name that I would have a hard time at guessing and requires some spiffy looping to find. It won’t appear at all in the debugger either.
The first issue however comes when objects are created inside a function and you need to get a hold of them elsewhere. i.e.:
createShape();
trace(this.myShape)
function createShape():void {
var myShape:Shape = new Shape();
myShape.name = "myShape";
addChild(myShape);
}
Uh oh – undefined? The obvious solution is to create the variable for the shape outside of the function, but then we have to do the same for every single object we create and things that are meant to be easy have just become more complicated. Not fun.
What I like to do is group together objects in something separate from the foundation of the class. This can be easily done by just creating a reference to it from a single, empty, object container.
var _views:Object = new Object();
createShape();
trace(_views.myShape)
function createShape():void {
var myShape:Shape = new Shape();
myShape.name = "myShape";
_views[myShape.name] = myShape;
addChild(myShape);
}
Note that the _views object is not a MovieClip nor a Sprite that needs to be added. Its simply a container holding references to stuff I’ve dynamically added. The significance of this is that its now easy to not only create and reference objects I’ve made, but also to loop only those objects I’ve created and quickly get a list of them. Better yet, in the debugger, all I have to do is look at my _views object to see everything I’ve created that is currently available. To access the shape, I simply use _views.myShape.
Typically, I like to go one step further and separate out different groupings of objects that are not necessarily related. Maybe I only want to loop through squares, but not circles. There are plenty of options to achieve this via design patterns – make two separate classes perhaps – but I could also just create more than one _views containers. I could have _viewsSquares and _viewsCircles for instance. Most design pattern books will say this is a bad practice, but IMHO, over-complicating something simple is not always necessary in simple projects.
- AS3 My Way: Intro
- AS3 My Way: Name Everything!
- AS3 My Way: Adding, Removing, and Memory Usage
- AS3 My Way: A little bit of MVC
Have a question or comment?