We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
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
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!
No luck yet. I'll research variable scope like you so kindly suggested, TfGuy44.
What are setup() and draw()?
And no point in reposting code, just edit your initial message and change it.