How do you create a button that changes states in a multi-state sketch?

I created a button that is supposed to change the current state to the next state, but it does not work...please help!

int state = 1;

//button processing forum int x; int y; int w; int h; //button processing forum

void setup() { size(1200, 1200); smooth(); background(255); }

void draw () {

//processing examples state change if (state == 1) { title(); } else if (state == 2){ americamain(); } else if (state == 3){ austriamain(); } else if (state == 4){ americanstart(); } else if (state == 5){ austrianstart(); } else if (state == 6){ americarace(); } else if (state == 7){ austriarace(); } else if (state == 8){ americafinish(); } else if (state == 9){ austriafinish(); } else if (state == 10){ title(); }

}

void americamain() { int x = 600; int y = 1100; int w = 100; int h = 50;

background(0, 175, 255, 255);

fill(255); noStroke(); quad(150, 800, 600, 650, 1050, 800, 550, 1000); rect(0, 800, 1200, 400);

//button processing forum fill(0); rect(x,y,w,h); if (mousePressed) { if(mouseX>x && mouseX<x+w && mouseY>y && mouseY <y+h){ state ++; if (state > 2) state = 4; } } } void americanstart() {

int x = 100; int y = 500; int w = 100; int h = 50;

background(0, 175, 255, 255);

fill(255); noStroke(); rect(0, 800, 1200, 500);

//button processing forum fill(0); rect(x,y,w,h); if (mousePressed) { if(mouseX>x && mouseX<x+w && mouseY>y && mouseY <y+h){ state ++; if (state > 4) state = 6; } } }

void americarace() {

int x = 300; int y = 900; int w = 100; int h = 50;

background(0, 175, 255, 255); fill(255); rect(0, 700, 1200, 500); fill(255, 0, 0); rect(200, 500, 200, 100);

 //button processing forum

fill(0); rect(x,y,w,h); if (mousePressed) { if(mouseX>x && mouseX<x+w && mouseY>y && mouseY <y+h){ state ++; if (state > 6) state = 8; } } }

void americafinish() {

int x = 600; int y = 800; int w = 100; int h = 50;

background(0, 175, 255, 255); //button processing forum fill(0); rect(x,y,w,h); if (mousePressed) { if(mouseX>x && mouseX<x+w && mouseY>y && mouseY <y+h){ state ++; if (state > 8) state = 9; } } }

Answers

  • Remember to hit ctrl-t in processing to get automatically format

    Also edit your previous post and format the code better:

    Gear | Edit

    Empty lines before and after the code

    Select entire code with mouse

    Hit ctrl-o

  • Instead of using the mousePressed variable please use the function mousePressed () - see reference

    . You can recognize the function mousePressed () by the brackets ()

    void mousePressed () {

    state ++;

    }

Sign In or Register to comment.