When I was just getting started in OOP, I would have trouble keeping track of, and organizing my Objects. For example, I had a GalleryThumbnail class that I would dynamically create for each item in an XML feed full of images and their thumbnails. I would then position add them to the stage. From there on out, I lost my reference to the individual Objects.
This caused me problems on all sorts of projects, from a few large video portals (with their playlist Objects) to a dynamic photo gallery (and their photo Objects). Since the individual Objects were being created & instanciated during a for loop, I would loose all reference to them once they were made. To solve this, I started putting my Objects in a holder Array.
Basically, after you add your Object to the stage in your for loop, you also push it into the container Array:
// Create your container Array
var container:Array = new Array();
// Drop a bunch of stuff into them
for(var u:int = 0; u < 5; u++){
var tempSprite:Sprite = new Sprite();
container.push(tempSprite);
}
This leaves you with an Array full of references to your dynamically created items. If you want to access them, you would simple use this:
trace(container[0]); // First object trace(container[1]); // Second object
You could even pass this to Tweener to animate your Object:
Tweener.addTween(container[0], {x:10, y:10, time:1, transition:"linear"});
Now that you have a reference to your Objects, you can access them at any time.
I am working on an addition to com.dyc (my AS3 Class Library) that’s an Object organizer class. Pretty much a souped up version of a holder Array, but for every type of Object you could want to store for a project. More on that soon!
If you have any suggestions for efficient ways to store & access your Objects, leave them in the comments.











Subscribe to our RSS feed!