Ways to reference containers created in one class in other classes?
I want to find an alternative to passing containers by reference into movieclips as function parameters. What I currently have works, but as I keep making more containers and have to pass through more and more to movieclips that require them, I have a ridiculous number of function parameters for most of my movieclip objects and it's getting difficult to manage.
Here's an abbreviated example of what I'm currently doing (keeping in mind this is for a video game and the "Engine" class represents just one level; a Main class calls in the different levels).
package
{
//imports
public class Engine extends MovieClip() //This is the main engine class that runs the game level
{
private var _stage:Stage; //an instance of the stage
public static var thingCont:MovieClip;
public static var stuffCont:MovieClip; //The two containers that'll be holding movieclips
private var _thing:MovieClip; //An object that'll actually show up on the stage
public function Engine($stage:Stage)
{
_stage = $stage; //passed in by reference from Main class
thingCont = new MovieClip(); //Where I'll store the thing
stuffCont = new MovieClip(); //Where I'll store the stuff
_stage.addChild(thingCont);
_stage.addChild(stuffCont); //Both containers belong to the Stage now
_thing = new Thing(_stage, thingCont, stuffCont);
thingCont.addChild(_thing); //Now the thing is in the thingCont, which makes it go right to the stage
}
}
}
package
{
//imports
public class Thing extends MovieClip()
{
private var _stage:Stage;
private var _thingCont:MovieClip;
private var _stuffCont:MovieClip;
private var _stuff:MovieClip; //"Thing" is going to create an instance of "stuff" here that needs to be added to stuffCont, which is owned by the stage
public function Thing($stage:Stage, $thingCont:MovieClip, $stuffCont:MovieClip)
{
_stage = $stage;
_thingCont = $thingCont;
_stuffCont = $stuffCont; //these 3 are passed in by reference from Engine class and are indeed accessible, no problem
_stuff = new Stuff(//etc.);
_stuffCont.addChild(_stuff); //_stuff is now a child of _stuffCont, which is a reference to stuffCont in the Engine, which belongs to the stage, so it's correctly added to the stage
}
}
}
So the problem is in my actual program, "thing" needs to create a whole lot of different kinds of movieclips (not just 1 as in this example), many that are requiring different containers. A lot of THOSE movieclips will even be creating their own new movieclips. Again, the way I'm currently doing it by passing containers through the functions works fine, but I'm finding myself having to pass in SO many containers and it's getting very hectic to manage. My question is whether it might be possible to bypass this? Maybe by using my instance of _stage to access those containers? When I do the following in "Thing":
for(i=0; i<_stage.numChildren; i++)
{
trace(_stage.getChildAt(i))
}
to see all of the stage's children (which includes the stuffCont and thingCont), it just returns [object MovieClip] [object MovieClip], meaning Thing doesn't seem to be reading those containers by their names, just their types.
Ideally, I would like to do something like the following:
public function Thing($stage:Stage):void //minimal parameters, doesn't that look nice?
{
_stage = $stage;
_thingCont = _stage.thingCont;
_stuffCont = _stage.stuffCont;
//OR something like this...
_thingCont = _stage.getChildAt(//thingCont index);
_stuffCont = _stage.getChildAt(//stuffCont index);
//But neither one of these tricks work...
}
Basically just access everything by the stage instance, or something similar. Any suggestions?
