Why can't I access my variables from void Draw in void mousePressed?

edited April 2015 in Questions about Code

Hello, I've got my code listed below. I'm definitely a rookie. In the .txt files, I have the 1st and 2nd half of simple multiplication questions. I hope to:

(a) display both halves in white
(b) after the user clicks, highlight either the 1st half or the 2nd half of the question (a randomly generated decision). (c) pause for 2 seconds (d) display the next randomly generated equation (return to (a))

I can get (a), (c) and (d) to work, but (c) is giving me a lot of trouble. The text commands don't seem to work at all, and the println (just for testing purposes) displays different numbers than it should be.

Also, it seems to need to put the String commands in both the void Draw section AND the void mousePressed section? I'd like to be able to use the generated variables from Draw in the mousePressed section.

Does anyone have any ideas? Thanks so much!

int choice = 0; int randy = 0; long lastTime = 0;

void setup() { size (640, 360); background(0); stroke(100); PFont f; f = createFont("Arial",16,true); // Arial, 16 point, anti-aliasing on noLoop(); }

void draw() { background(0);

String factors_mult[] = loadStrings("factors_mult.txt"); String products_mult[] = loadStrings("products_mult.txt");

fill(255); textSize(width/10); int randy = int(random(0,factors_mult.length)); fill(255); textAlign(RIGHT); text(factors_mult[randy],width2/3, height/2); textAlign(LEFT); text(products_mult[randy],width2/3, height/2);

choice = int(random(2));
println (choice);

noLoop();

}

void mousePressed() { String factors_mult[] = loadStrings("factors_mult.txt"); String products_mult[] = loadStrings("products_mult.txt");

lastTime = millis();

if (choice < 1) { fill(255, 0, 0); textAlign(RIGHT); text(factors_mult[randy],width2/3, height/2); println (factors_mult[randy]); println ("Factor"); } else { fill(0, 255, 0); textAlign(LEFT); text(products_mult[randy],width2/3, height/2); println (products_mult[randy]); println ("Product"); }

for (int i = 0; i < 10; i++) { i = 1; if (millis()-lastTime > 2000) { i = 10; } } loop(); }

Answers

  • Answer ✓

    You'll get better answers if you format your code with these instructions

    http://forum.processing.org/two/discussion/8045/how-to-format-code-and-text#latest

  • Answer ✓

    If you define a variable inside your draw function, that variable can only be accessed from inside your draw function!

    If you define it outside of your functions, it is global, and then can be accessed from inside all functions.

    This is known as variable scope, and it's a good topic to research if you want to learn more.

    Oh, and you have loadStrings() inside draw, Which is so blatantly wrong that I AM NOW GOING TO SHOUT AND USE BOLD TEXT BECAUSE THIS IS SUPER WRONG AND A PET PEEVE ON MINE! DO ALL YOUR LOADING IN SETUP!

  • Thanks bilmor! I've attempted to post the code again below. And thank you TfGuy44! I'm going to try your advice!

    int choice = 0;
    int randy = 0;
    long lastTime = 0;
    
    void setup() {
      size (640, 360);
      background(0);
      stroke(100);
      PFont f;
      f = createFont("Arial",16,true); // Arial, 16 point, anti-aliasing on
      noLoop();
    }
    
    void draw() {
      background(0);
    
      String factors_mult[] = loadStrings("factors_mult.txt");
      String products_mult[] = loadStrings("products_mult.txt");
    
      
      fill(255);
      textSize(width/10);
        int randy = int(random(0,factors_mult.length));
        fill(255);
        textAlign(RIGHT);
        text(factors_mult[randy],width*2/3, height/2);
        textAlign(LEFT);
        text(products_mult[randy],width*2/3, height/2);
        
        choice = int(random(2));
        println (choice);
            
        noLoop();
    }
    
    void mousePressed() {
      String factors_mult[] = loadStrings("factors_mult.txt");
      String products_mult[] = loadStrings("products_mult.txt");
    
      lastTime = millis();
      
      if (choice < 1) {
        fill(255, 0, 0);
        textAlign(RIGHT);
        text(factors_mult[randy],width*2/3, height/2);
        println (factors_mult[randy]);
        println ("Factor");
      } else {
        fill(0, 255, 0);
        textAlign(LEFT);
        text(products_mult[randy],width*2/3, height/2); 
        println (products_mult[randy]);
        println ("Product");
      }
      
      for (int i = 0; i < 10; i++) {
        i = 1;
        if (millis()-lastTime > 2000) {
          i = 10;
        }
      }
      loop();
    }
    
  • No luck yet. I'll research variable scope like you so kindly suggested, TfGuy44.

  • edited April 2015 Answer ✓
    • In Java there are basically 2 scopes: fields & local variables: ~O)
    • And curly braces create scopes. Anything declared in a scope has the lifespan of that scope!
    • Pay attention on the declare part. Declare a variable isn't the same as assign a value to it.
    • In order to declare a variable in Java, we specify its datatype at the left side of the variable's name.
    • Then we can assign values to it afterwards.
    • Watch out for re-declaration of the same variable name later on! Source of nasty bugs!


    int global_field = 10; // field or "global" variable. Exists within the sketch's class.
    
    {
      int local_block = 20; // this variable only exists within these curly braces!
    }
    
    void setup() {
      int local_method = 30; // this variable only exists within setup() method!
    
      println(global_field); // displaying the content of the sketch's field.
      println();
    
      for (int i = 0; i != 10; ++i) { // iterator i only exists within this for () loop block!
        displayVal(i);
      }
    }
    
    static final void displayVal(int val) { // parameters are also local variables!
      println(val);
    }
    
  • Answer ✓

    What are setup() and draw()?

    And no point in reposting code, just edit your initial message and change it.

Sign In or Register to comment.