Let me see if I got it right…
You have an array w/ 6 buttons and a boolean array w/ 2 states.
Each button makes 1 of the states true
and the other false
when clicked.
So basically you need an extra array which stores the indices to be set to true
when their corresponding button is clicked:
STATE_INDICES = Uint8Array.of(1, 0, 1, 1, 0, 1)
The function createButton() has a 2nd parameter value which we can use it to store the index we want to be set to true
for that button:
createButton(label, [value])
In order to read a button’s stored value we can invoke the method p5.Element::value(), given a button is of datatype p5.Element:
Inside the mousePressed()'s event callback we need to use the keyword this
in order to access the clicked button.
The only gotcha is that p5.Element::value() returns a string, so we need to use the unary plus +
operator to convert it to a number: +this.value()
After that we can use that value as the index for the boolean array buttonStates[]:
buttonStates[+this.value()] = true;
Here’s the full button event callback:
function changeButtonStates() {
buttonStates.fill(false);
buttonStates[+this.value()] = true;
redraw();
}
And this is the complete example demo for it: