How to use data in one 'if' statement to another.
in
Programming Questions
•
7 months ago
Hi all,
Possible beginner question/problem here.
I set-up a float that is being produced by a random number in a while statement, and set it up, so that given a certain condition, a new random number will be constructed. But when I try to reference that float, c1 in this case, in another if statement, it cant "find" the original c1 called in the while statement.
The way the code is supposed to work is that the code produces and outputs and random number. When the user holds the 'q' key and matches the X-position (with an error of +/- 5 px) of the mouse with the random number, they get a score, and a new number is generated.
The problem prevents me from creating a checking system if the user is in range, thus preventing the creation of the next random number.
Thank you in advance:
Possible beginner question/problem here.
I set-up a float that is being produced by a random number in a while statement, and set it up, so that given a certain condition, a new random number will be constructed. But when I try to reference that float, c1 in this case, in another if statement, it cant "find" the original c1 called in the while statement.
The way the code is supposed to work is that the code produces and outputs and random number. When the user holds the 'q' key and matches the X-position (with an error of +/- 5 px) of the mouse with the random number, they get a score, and a new number is generated.
The problem prevents me from creating a checking system if the user is in range, thus preventing the creation of the next random number.
Thank you in advance:
- void setup() {
- background(255);
- size(500, 500);
- noStroke();
- }
- int state=0;
- int score=-1;
- int q;
- void draw() {
- while (state==0) {
- background(255);
- float c1 = random(50, 256);
- text(c1, 50, 50);
- score+=1;
- text(score,100,50);
- state+=1;
- }
- if (keyPressed) {
- if (key== 'q') {
- q=mouseX;
- }
- }
- if((c1-5)<q && q<(c1+5)){
- state=0;
- }
- }
1