View Full Version : 10 buttons, only 1 visible
up-
July 6th, 2003, 08:42 PM
Okay, ive got another problem.... i have 10 buttons (b1,...,b10) and want to set their default visible property to false. I want only 1 of those mcs (pick by random) to be visible. How i do that?
Thanks
ahmed
July 6th, 2003, 08:48 PM
button.prototype._visible = false
rand = Math.round(Math.random()*10)
this["b"+rand]._visible = true
that i think does it
up-
July 6th, 2003, 08:52 PM
No Ahmed, didnt work...
h88
July 6th, 2003, 09:00 PM
If ahmed's way didn't work, then probably your Buttons are behaving as Movieclips, try this instead:
MovieClip.prototype._visible = false
this["b"+(Math.floor(Math.random()*10)+1)]._visible = true
lostinbeta
July 6th, 2003, 09:03 PM
Ahmeds way should work, you just have to make sure your button instance name is b1, b2, b3, etc.
up-
July 6th, 2003, 09:09 PM
Didnt work. Ive tried both ways.
My buttons are not behaving as movie clips.
I have 10 buttons on stage (b1,...,b10) and placed the code on 1st frame of my timeline.
All buttons were still visible.
lostinbeta
July 6th, 2003, 11:47 PM
Well this script is assuming you set all your buttons as invisible first.
Example...
//cycle through buttons to make them all invisible
for (var i = 1; i<=10; i++) {
this["b"+i]._visible = false;
}
//choose random number from 1 to 10
var rand = Math.floor(Math.random()*10+1);
//make 1 button visible again
this["b"+rand]._visible = true;
lostinbeta
July 6th, 2003, 11:52 PM
And a slight variation...
//choose random number from 1 to 10
var rand = Math.floor(Math.random()*10+1);
//button which will stay visible
var keepVisible = this["b"+rand];
//cycle through buttons to make them all invisible
for (var i = 1; i<=10; i++) {
//if current button is not the keepVisible button
if (this["b"+i] != keepVisible) {
//make it invisible
this["b"+i]._visible = false;
} else {
//else keep it visible
this["b"+rand]._visible = true;
}
}
NOTE: You can remove the else {
//else keep it visible
this["b"+rand]._visible = true;
} and it will still work exactly the same, I just felt it was easier to explain with it in there.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.