How to use draw in Class?

Hi, there. I have a question about using class. I can use only one 'draw' but i would like to mix several examples. How I can use draw in class? Or is there any way to get class instead of draw?

for example, I would like to make, when I press '1' -> show example 1. when I press '2' -> show example 2. like this.

Thank you for reading this.

void keyPressed() {
  int keyIndex = -1;
  if (key >= 'A' && key <= 'Z') {
    keyIndex = key - 'A';
  } else if (key >= 'a' && key <= 'z') {
    keyIndex = key - 'a';
  }
  if (key== '1') {
    bground.update();
    bground.display();

  } 
  if (key == '2'){


  }
  else {

  //bg.fill(255);
  //bg.rect(0,0,1280,720);
    fc001=0;
    pg.background(255);
    pg.clear();

    pg.text(key, random(100,width-100), random(250,height-250));
  }
}
Tagged:

Answers

  • Answer ✓

    Use some internal sketch state.

    int state = 0;
    
    void setup(){
      size(220,220);
    }
    
    void draw(){
      if( state == 0 ){
        background(0);
      } else if( state == 1 ){
        background(255,0,0);
      } else if( state == 2 ){
        background(0,255,0);
      }
    }
    
    void keyPressed(){
      if( key == '1' ) state = 0;
      if( key == '2' ) state = 1;
      if( key == '3' ) state = 2;
    }
    
  • Wow....thanks a lot! I will try with this code!!!!!!!! Appreciate a lot!!!

Sign In or Register to comment.