How to make text boxes?

edited September 2014 in How To...

I would like to add a sort of sign in affect to my game ( which by the way is a huge success) like I want to be able to add a place were you can type something, press enter, then have it take me to my game. I hope that is possible :))

Answers

  • Answer ✓

    Sure. Use states.

    int state = 0;
    String s="";
    
    void setup() {
      size(220, 220);
    }
    
    void draw() {
      if (state==0) {
        background(0);
        fill(255);
        text("Type \"start\" to start!", 20, 20);
        fill(255, 255, 0);
        text(s, 20, 50);
      }
      if (state==1) {
        background(0);
        fill((millis()+100)%255, (millis()+200)%255, millis()%255);
        ellipse(mouseX, mouseY, 20, 20);
      }
    }
    
    void keyPressed() {
      if (state==0) {
        if (key>='a'&&key<='z') {
          s+=key;
        }
        if (key==ENTER||key==RETURN) {
          if (s.equals("start")) {
            state = 1;
          }
          s="";
        }
      }
    }
    
    void mousePressed(){
      if(state==1){
        state=0;
      }
    }
    
  • Again, don't forget to use a category. Moved.

    For simple input, the methods shown above is enough. If you start to have complex needs, use a GUI library like G4P, ControlP5 or similar.

  • Okay, thanks. Very much

  • But what is s.equals?

  • s.equals("start")

    this compares the contents of the string s with the character sequence 's', 't', 'a', 'r', 't' and returns true if there is an exact match (including case).

  • Thank you quark. You are a nice person. Anyways I know I'm a bit exasperating but finally so I can get all this, what is a string?

  • Remember my advice to search in the Reference page? Don't forget to click on the links you find, too...

  • Than you all, mostly PhiLHo and Quark. These are great people and I feel privileged to receive advice from them.

  • edited September 2014

    Hello,

    I assume you want the player to enter his/her name and then the name is displayed during the game.

    As you know we use variables to store stuff we need later, like

    int widthRect = 5; 
    

    The type here is int. The type tells processing, this variable is an integer number or a float number or a boolean etc. See reference.

    For storing a word like TechWiz the type is String. See reference.

    So we could say

    String playerName = "TechWiz";
    

    Please note the important " " signs.

    I assume you want the player to enter his name and then the name is displayed during the game.

    this would be this sketch

    You have to build it into your sketch. There you probably already have states for start screen etc. You probably also have setup() and draw() already so you need to read my sketch, understand it and copy the parts you need into yours...

    Best, Chrisir ;-)

    // the states of the program like start 
    // screen / pause screen / enter name screen
    // we start by giving the states names 
    final int stateReadName = 0; // const 
    final int statePlayGame = 1;
    int state = stateReadName;   // current 
    
    String playerName="";
    
    void setup() {
      size(220, 220);
    }
    
    void draw() {
      if (state==stateReadName) {
        background(0);
        fill(255);
        text("Type your name and press enter:", 20, 20);
        fill(255, 255, 0);
        text(playerName, 20, 50);
      }
      else if (state==statePlayGame) {
        background(0);
        fill((millis()+100)%255, (millis()+200)%255, millis()%255);
        ellipse(mouseX, mouseY, 20, 20);
        fill (255);
        text (playerName + " is playing.", 10, 12);
      }
    }
    
    void keyPressed() {
      if (state==stateReadName) {
        // what key was it?   ---
        if ( (key>='a'&&key<='z') || ( key >= 'A'&&key<='Z')) {
          playerName+=key; // add this key to our name
        } // Letter 
        else if (key==ENTER||key==RETURN) {
          // go on to game
          state=statePlayGame;
          println ("Thank you, "+playerName+".");
        } // ENTER
        else if (key==BACKSPACE) {
          if (playerName.length()>0) {
            playerName=playerName.substring(0, playerName.length()-1);
          }
        } // BACKSPACE
        else { 
          println ("Unknow key "+ key);
        } // else
        // end of check for keys ---
      } // if state
    }
    
    void mousePressed() {
      // return to enter name
      if (state==statePlayGame) {
        state=stateReadName;
      }
    }
    //
    
  • Thank you Chrisir. Please check out my new post

Sign In or Register to comment.