Help with small program and mousePressed
in
Programming Questions
•
7 months ago
Hello everyone, hopefully i can get some help here as I am a beginner and quite frankly I am stumped. I hope that someone can help me or point me in the right direction.
What I am trying to do is create a small reflex testing program in which a square alternates between 6 different colors for a set period of time. Each time the color of the square is red, the user clicks their mouse. The program keeps track of how many times the user successfully clicked while the square was red, and at the end of the time allocation it displays the results.
I think I have everything in the program covered, except there is one problem that I am having. The program only will add to my "successful clicks" count if I am actually holding the mouse button down while the square turns red, and not if I actually click the mouse while the square is ALREADY red. I am suspecting it has to do something with the way I used mousePressed. I have attached the program if anyone is interested in taking a look and maybe pointing me in the right direction.
Recap: I need the program to keep count of how many times I successfully click the mouse while the square is red. As of now, it only recognizes a successful click if the mouse is already being held down upon the color switching to red.
Recap: I need the program to keep count of how many times I successfully click the mouse while the square is red. As of now, it only recognizes a successful click if the mouse is already being held down upon the color switching to red.
Thanks for any advice in advance!
- int r = 0;
- int m = 0;
- void setup() {
- size(200,200);
- background(255);
- stroke(0);
- rectMode(CENTER);
- ellipseMode(CENTER);
- frameRate(2);
- }
- void draw() {
- //Start rectangle after 1 second and end after 60 seconds
- if (millis() > 1000 && millis() < 8000) {
- //Set-up random colors
- int c = int(random(1,6.9));
- if (c==1) {
- fill(255,0,0); //Red
- } else if (c==2) {
- fill(0,44,255); //Blue
- } else if (c==3) {
- fill(19,170,0); //Green
- } else if (c==4) {
- fill(219,240,0); //Yellow
- } else if (c==5) {
- fill(255,3,209); //Pink
- } else if (c==6) {
- fill(0); //Black
- }
- //Draw rectangle
- rect(100,100,100,100);
- //Add to Red-Count
- if (c==1) {
- r = r + 1;
- }
- //Add to Mouse-Count
- if ((mousePressed) && (c==1)) {
- m = m+1;
- }
- }
- if (millis() > 8001) {
- background(255);
- fill(255);
- stroke(255);
- rect(100,100,10,10);
- println("Number of times rectangle was red:"); println(r);
- println("Score"); println(m);
- noLoop();
- }
- }
1