Copy link to clipboard
Copied
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?
Without getting too into it, it seems like a singleton class might be of help. You could store all your containers in it and then just grab whichever one you needed. Also, one thing I always do is to have a setter in each of my views/screens that allows me to set the container: public function set container(c:DisplayObjectContainer):void{ myContainer = c;} and then Main creates and manages the containers.
Copy link to clipboard
Copied
what does Thing do? ie, what's its purpose for existing?
Copy link to clipboard
Copied
Well... I just wrote this code up as an example to illustrate my question as concisely as possible without delving into the specifics. This program already spans 30 classes and the Engine code alone is over 1000 lines of code so I was trying to simplify this question as much as I could. "Thing" isn't actually in my code, it is representative of one of many different movieclip objects I call in my Engine class often.
For one example, though, I call 15 different enemy units from the Engine class (using a for loop, and I store all of the enemies in "_enemyContainer"). Each of those enemies is responsible in their own class for producing lasers to shoot at the player, which go into the "_enemyLaserContainer". I have the code to add enemy lasers to the stage inside the enemy unit class because I don't want to clutter up the Engine code any more than it already is, plus all I have to worry about in the Engine class is simply adding the enemy unit then the enemy unit class automatically has all of the functions within it needs to function properly.
So, I create the enemy and pass in the _enemyLaserContainer into it so that it can add lasers to the stage. Why don't I just create the _enemyLaserContainer within the enemy unit class? Because I have several different enemy types added through the Engine class and I want them to all add their lasers to the same _enemyLaserContainer. I often use many of my containers in many different movieclips, so I keep track of all of them by adding them all at the same time in the same place in the Engine code so they can easily be passed into every movieclip object that needs to use them.
Let me know if you need any more clarification.
Copy link to clipboard
Copied
Without getting too into it, it seems like a singleton class might be of help. You could store all your containers in it and then just grab whichever one you needed. Also, one thing I always do is to have a setter in each of my views/screens that allows me to set the container: public function set container(c:DisplayObjectContainer):void{ myContainer = c;} and then Main creates and manages the containers.
Copy link to clipboard
Copied
dmennenoh- thanks for the input! I had not heard of singleton classes before your post but after some research I do believe this is what I was looking for. It's something to store all the containers in that I can access universally in all my other classes. I haven't had a chance to try it out yet but I appreciate the lead!
Edit: Tried it out using the below tutorial on singletons and it worked BEAUTIFULLY. I'm so happy, this is going to make my life so much simpler from now on. Thanks again for the help!!
http://code.tutsplus.com/tutorials/quick-tip-the-singleton-pattern--active-4343
Find more inspiration, events, and resources on the new Adobe Community
Explore Now