this is my first post here so please bear with me.
I'd like to be able to have an array of movieclips where i'm able to toggle their visibility depending on what button has been pressed. I would need to have a default movieclip that is visible and all the others are hidden, but then when I click a button to make one of the other movieclips visible the default one is hidden etc.
is this example any help to you ? it is in AS2 (you haven't specified your version)
PHP Code:
cArr = [clip0, clip1, clip2]; // instance names of movieclips
for(var a in cArr)cArr[a]._visible = false; // hide the array
default_btn.onRelease = function(){ // instance name of button
for(var a in cArr)cArr[a]._visible = true; // show the array
};
function hideMC(clip){
for(var a in cArr)cArr[a]._visible = false; // hide all
clip._visible = true; // show clicked clip
default_btn._visible = false; // hide default
}
for(var a in cArr){ // loop through array
cArr[a].onRelease = function(){ // adding button events
hideMC(this);
trace(this); // open output window - show which clip
};
}
Thanks for your reply a_modified_dog but it's not working like I need it to.
I have attached some images below to hopefully explain.
The movieclips are the pink circles. Each movieclip has a button assigned to it to make it visible only. When a different button is clicked it sets it's movieclip visible and all the others invisible.
This is the default state when first loaded.
If I click on button 2 the first movieclip is hidden and the second one is visible
And so on...
...and on.
Once the default state is invisible it should work just like the other buttons so if I clicked back on to button 1 the default movieclip (clip1) should be visible.
Does this make more sense? I can get each button to turn its movieclip on and off ok but I have to do each one manually and this isn't what I need.
Last edited by Giraffe Media; 11-11-2009 at 07:14 AM.
I have managed to get it working as I need it now, but I have had to apply code to each button and movie clip to show hide each mc. I could complete the project like this, but I may have over a hundred mc's in total so i'd rather use actionscript to control an array than have to attach it to each button/mc combination like I have done here.
seeing the images above, i used this code in a test file -
PHP Code:
btnArr = [btn1,btn2,btn33,btn56]; // note: button and clip instance
clipArr = [clip1,clip2,clip33,clip56]; // names all end in numbers
// hide all of the clips
for(var a in clipArr) clipArr[a]._visible = false;
// find number of clicked button
// so click button99, = return number 99
function extractNumber(str){
var b = str.length, c, tmp = "";
for(var aa=0; aa<b; aa++){
c = str.charAt(aa)
if(c==parseInt(c)){ // if it is a number
tmp += c;
}
}
return parseInt(tmp); // return the number
};
for(var d in btnArr){ // set button events
btnArr[d].onRelease = function(){
num = extractNumber(this._name); // get the number
for(var e in clipArr) clipArr[e]._visible = false; // hide all clips
this._parent["clip"+num]._visible = true; // add the number to the clip and show it
}
}
It certainly did thanks very much. I had to add a line of code to get one of the clips visible by default, but apart from that it works like a treat!
I have also tested duplicating the code, buttons and mc's and changing the array names and it works fine. Chances are I will need several arrays of 10+ items and this method is a very lightweight way of doing what I need. I was dreading having to hard code each button and mc!
Thanks very much a_modified_dog, you've been a great help.
James
Last edited by Giraffe Media; 11-12-2009 at 05:34 AM.