How to turn off one object and turn on another with one click in an array?

Honestly, the title probably doesn’t make much sense (I’m new to this), but I’m in a tough spot. I’m a student and one of our modules is processing, a quick 2-week stint. I literally had two classes on processing and we are left to our own devices to come up with something on our own.

What I have (or believe I have) is an array of on/off switches. What I’m trying to do is make one switch turn off, and in doing so turn on another. It’s essentially a never-ending “game”. The problem I’m facing right now is trying to get the code to make these switches communicate to each other by turning off one switch and turning on another, either randomly or defined.

Does anyone have any idea how to do this? I can try and explain things better if needed. I will include my code here. It is a mess I’m sure and constructed in an awful way, but its new to me. Any help would be appreciated, I just wish I had more time to get to learn p5/processing.

1 Like

Sure once a cell gets clicked, have a for loop to switch all cells off, then switch the clicked one back on

I’ve had a go at this for a while but I’m not sure what the best way to tackle it is? Where should the for loop go? And what should the for loop contain?

I was thinking something like; for (switch from array 1 is pressed; press random(0,5) switch from array as well).

I’m slowly getting to understand the terminology of everything but you’ll have to forgive me as I still don’t have a great understanding of it yet.

Well does one button know whether it’s on or off? And is drawn accordingly?

Then change this in the for loop to off

Here’s what you can do.

Have a list called switches that contain ALL switches, no matter what their state is.

If the user clicks a switch on, then there’s no need to do anything.

Now if the user clicks a switch off, then we must turn another on. For the sake of making it feel nicer, let’s make it switch back on a random one.

So to do this you can simply loop through your switches list and collect all switches that are off and make a new list of out them. Now you have a new list with all switches that are off. With that new list you can simply use random to pick a random switch to turn on, and that’s it.

Make sense?

1 Like