We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi there,
I made an array of buttons using a class written. Buttons work fine but not turning off when I click on one of the other buttons in the array.
Any help would be much appreciated Many thanks in advance :)
Code
//////////////////////
Button [] b ;
void setup() {
size(800, 800);
smooth();
b= new Button[5];
for (int i =0; i<b.length; i++) {
b[i]= new Button(i*width/b.length, 0, width/b.length, height/18);
}
}
void draw() {
background(0);
for (int i =0; i<b.length; i++) {
b[i].display();
}
}
void mousePressed() {
for (int i =0; i<b.length; i++) {
b[i].interaction();
}
}
//////////////////////////////
class Button {
float posX, posY, bWidth, bHeight;
boolean status;
Button(float _x, float _y, float _bWidth, float _bHeight) {
posX = _x;
posY = _y;
bWidth = _bWidth;
bHeight = _bHeight;
}
void interaction() {
if (mouseX>posX&&mouseX<posX+bWidth&&mouseY>posY&&mouseY<posY+bWidth) {
status = !status;
}
}
void display() {
noStroke();
if (status) {
fill(45, 45, 200);
} else {
fill(45);
}
rect(posX, posY, bWidth, bHeight);
}
}
Answers
The 2nd line in method interaction()
status=false;
switches each button off before checking it.@Chrisir perfect! works well Thank you :)
@Chrisir perfect! works well Thank you :)
thank you!
in the last version though, the active button gets switched off unfortunately when you click outside any button.
In this new version the button stays ON.
@Chrisir I was about to mention this :) but you did it already awesome
Many thanks :)
You're welcome!
;-)
I'd be tempted to write a radio button set class which took a bunch of buttons and made sure that only one of them was chosen at any given time. (It's not much more than an ArrayList)