Skip to main content
Participant
March 25, 2011
Answered

Can I group objects to apply functions more simply?

  • March 25, 2011
  • 4 replies
  • 908 views

I have sets of movie clips and buttons (we can call them mc1, mc2 and btn3 for now), and I would like to be able to hide or show them all at once. Right now I have functions where I apply visibility changes to them one at a time, where the function contains

mc1.visible = false;

mc2.visible = false;

btn3.visible = false;

It works fine that way. However, for various reasons (including that my groupings have many more than 3 objects) it would be much better if I could define a group once (say "group1", containing mc1, mc2 and btn3) and apply things to them all at once, where group1.visible = false would be equivalent to the above code.

Because of other pieces of my code and the way I need to group them with some overlap (think venn diagram), it does NOT make sense to combine these objects into a movie clip called "group1" and then show/hide them all at once that way.

Is there any way I can do this, or will I have to keep writing stupidly long functions? Why it sucks is that right now if I want to add something to the hypothetical group 1, I have to find every function I try to apply to that grouping and add my btn or movieclip to the list. It would be great if I could just designate it as part of group1, and then any functions I have applied to group1 would affect it too.

Hopefully this makes sense. It just seems like there has to be a better way, but maybe there isn't.

This topic has been closed for replies.
Correct answer Ned Murphy

Here's an example for what I described where a objects of a group are made visible/invisible based on the current status each holds for the visible property.  They do not all have to have the same value for that property, meaning you could be making some of them appear and others disappear... that's just specific to this example.

var group1:Array = new Array(mc1,mc2,mc3,mc4);

function toggleGroupVisibility(grp:Array){
for(var i:uint=0; i<grp.length; i++){
  grp.visible = !grp.visible;
}
}

toggleGroupVisibility(group1);

-----------------------------------

You could just as easily just set the visible property to a specific state for the group....

function toggleGroupVisibility2(grp:Array,stat:Boolean){
for(var i:uint=0; i<grp.length; i++){
  grp.visible = stat;
}
}

toggleGroupVisibility2(group1, false);  // make them all invisible

toggleGroupVisibility2(group1, true);  // make them all visible

4 replies

spilledAuthor
Participant
March 25, 2011

Ned, that's super helpful. Thanks so much! I hadn't thought of looping through like that. (I'm a self-taught scripter and loops are my gray area of understanding, but once I get them to work I'm always glad I did it that way.)

Ned Murphy
Legend
March 25, 2011

You're welcome.

Ned Murphy
Ned MurphyCorrect answer
Legend
March 25, 2011

Here's an example for what I described where a objects of a group are made visible/invisible based on the current status each holds for the visible property.  They do not all have to have the same value for that property, meaning you could be making some of them appear and others disappear... that's just specific to this example.

var group1:Array = new Array(mc1,mc2,mc3,mc4);

function toggleGroupVisibility(grp:Array){
for(var i:uint=0; i<grp.length; i++){
  grp.visible = !grp.visible;
}
}

toggleGroupVisibility(group1);

-----------------------------------

You could just as easily just set the visible property to a specific state for the group....

function toggleGroupVisibility2(grp:Array,stat:Boolean){
for(var i:uint=0; i<grp.length; i++){
  grp.visible = stat;
}
}

toggleGroupVisibility2(group1, false);  // make them all invisible

toggleGroupVisibility2(group1, true);  // make them all visible

Participating Frequently
March 25, 2011

or put the movieclips or btns to movieclip container and then refer to container instance.

Ned Murphy
Legend
March 25, 2011

( It was already explained why that isn't an option: "Because of other pieces of my code and the way I need to group them with some overlap (think venn diagram), it does NOT make sense to combine these objects into a movie clip called "group1" and then show/hide them all at once that way." )

Participating Frequently
March 25, 2011

I know that your way is robust and clever, and I didnt even think about comparision to main, and I just put an alternative, when somebody unoriented, asking about basics, can merely digest things simple and familiar to him. But thanks for stressing this out, I wrote down a note for me with this useful array-grouping. It is really flexible and cool.

Cheers.

//edit: and have to admit that I didnt get whole of the task - didnt read it top-down, so sorry for being messy.;)

Ned Murphy
Legend
March 25, 2011

You can always use an array for identifying the members of a group, and then loop thru that array to set properties for them.  While it is still processing them each individually, for your side of the effort it becomes a few lines of code to manage an enttire group.