Processing, button, void

Posting in the forum

Prior to posting hit ctrl-t in processing

After copy and paste your code, select the code section using the mouse and click </> icon in the small command bar please

Your button

  • First: nothing is allowed outside the if (state==0)......else if (state==1)....else if (state==2)....} construct in draw(). Because you don’t want to display or evaluate your button during the game.

  • Second Using mousePressed as a variable is difficult. Better evaluate your button in the function mousePressed() that you already have.

  • 3rd: With if(mouseX>x1 && .... you check whether the mouse has been clicked inside the button and not somewhere else or in which button it has been clicked.

  • 4th: When the button has been clicked and you want to start the game, set state to 1.

Example

void mousePressed() {
  switch(state) {
  case 0:

    float x1=150;
    float y1=200;
    float w1=220;
    float h1=50;

    if (mouseX > x1 && 
      mouseX < x1 + w1 && 
      mouseY > y1 &&
      mouseY < y1 + h1) { 
      // is over the rect of the button 

      state=1;
      reset();
    }
    break;

  case 1:
    b.jump();
    break;

  case 2:
    reset();
    state=0;
    break;
    //
  }//switch
}

Chrisir

1 Like