What's wrong?

Hi,

I just began with processing recently and am trying to make a program that runs through several states. It begins with a welcome-state, then should cycle through an experiment state (should be reloaded 30 times) and a Thankyou-state. I'm having not much info in my states yet, but I thought I'd better get this working, instead of making it too sophisticated and then the basics are not running. So that's already the problem. The basic states. I want to change from welcome-state to experiment state through a button. That works happily. Then I want to click 30 times on a button (displayed green) and after 30 clicks, the Thankyou-screen should appear. But what happens is that I click the mouse and in the console, processing is counting multiple trials per click, but well... I don't want to change the states with some clicks, but with 30 clicks exactly... Can someone help me? :(

boolean Start = false;
char State = 'W';
boolean button= false;
int trial = 1;

/*
State W = Welcome 
State E = Trials 
State T = Thankyou
*/

void setup () {
  size (300,300);
  smooth ();
}

void draw () {
   if (State == 'W') {
    Welcome();
  } else if (State == 'E') {
    background(255);
    Experiment ();
  } else if (State == 'T') {
    Thankyou ();
  }
}

void Welcome () {
  background (255);
  drawStart(); 
  StartPressed();
  StartClicked();
}

void drawStart () {
  fill(255,0,0);
  rectMode(CENTER);
  rect(50,50,50,50);
  fill(0);
  textAlign(CENTER);
  text("START",50,50);
 }

void StartPressed() {
  if (mousePressed && mouseX>50-25 && mouseX<50+25 &&mouseY>50-25 &&mouseY<50+25 && State == 'W') {
    Start = true;
  } else  {
    Start = false;
  }
}

void StartClicked() {
   if (Start == true) {
     State = 'E';
   }
 }

void Experiment () {
  fill (0,255,0);
  ellipse (50,50,50,50);
  if (button){
   if (trial <30) {
      trial = trial + 1;
      background(255);
      State = 'E';
      println(trial);
    } else if (trial == 30) { 
    State = 'T';
    }
}
}

void mousePressed() {
  if (mouseX>50-25 && mouseX<50+25 &&mouseY>50-25 &&mouseY<50+25  && State == 'E') {
    button = true;
  } 
} 

void Thankyou (){
  background (0);
  fill (255);
  text ("doesn't work... :(", 150,50);
  ellipseMode (CENTER);
  fill (0,255,255);
  ellipse (150,150,50,50);
  fill (0);
  ellipse (145,145,10,10);
  ellipse (155,145,10,10);
  line (145,160,155,155);
}

Answers

  • Oh I got it already :)

  • edited November 2013

    Don't mix mouse checks in draw() (that are detected once per frame, 60 times per second) and in mousePressed() (to prefer in your case).

    General advice too: you should be consistent in your usage of uppercase and lowercase letters. In general, we prefer to start variable names and function names with lowercase. Initial uppercase is preferred to class names. (I used to start function names with uppercase, but I abandoned this practice, and at least I was consistent within a sketch! :-) )

    Oh, and try to make more descriptive subjects, indicating what you try to do.

Sign In or Register to comment.