Help! Help! Please. mouseClicked

edited October 2015 in Questions about Code

Hi,

I am looking for someone who can help me with a problem. Its a beginner problem, but I am learning.

I want to make a menuscreen before the game will start. So first you need to put your name on it and then you ll begin. but my problem is, that I can't select on a textbox for putting a name.

// huidige naam die je typt String naamSpeler1=""; String naamSpeler2=""; String savedPlayer1; String savedPlayer2;

void setup() { size(444, 444); background(51); }

void draw () { background(111);

// Tekst boven fill(0, 255, 163); // groen text("SPELER 1", 20, 60); text("SPELER 2", 20, 80);

// naam op vulhokje noFill(); stroke(255); rect (10, 115, 170, 20); rect (10, 200, 170, 20); // text fill(255); // white text(naamSpeler1, 20, 129); text(naamSpeler2, 20, 216);

//start knop noFill(); rect(305,300,60,20); textSize(15); text("START",310,315);

} void keyPressed () {

if (key == ENTER) {//heeft gekozen voor naam if (!naamSpeler1.equals("")|| !naamSpeler2.equals("")) { savedPlayer1 = naamSpeler1; naamSpeler1 = ""; println (savedPlayer1); savedPlayer2 = naamSpeler2; naamSpeler2 = ""; println (savedPlayer2); } } //else { // record the keys (in the current word) naamSpeler1 = naamSpeler1 + (key+""); naamSpeler2 = naamSpeler2 + (key+"");

} //else } // func //

its in dutch but you ll understand what I mean, if you run it.

Answers

  • Please read how to format code for this forum below: :-@
    http://forum.Processing.org/two/discussion/8045/how-to-format-code-and-text

    Quickest dirty solution is to use JOptionPane and its showInputDialog():
    http://docs.Oracle.com/javase/8/docs/api/javax/swing/JOptionPane.html

    If you intend to deploy it via "JS Mode" PJS you're gonna need a small utility library I wrote:
    http://forum.Processing.org/two/discussion/12532/windowjs-cross-mode-alert-confirm-prompt-other-js-api-for-java

    Then call window.prompt() to get the same effect as showInputDialog() but compatible w/ JS. *-:)

  • Additionally I recommend using states.

    The idea here is that you have a var state that tells you if your program is in mode nameEnter or in mode Game

    So in draw() you either paint the stuff for name entering OR the stuff for the actual game

  • Yes! actually its a begin of a game.

    So you first need to enter your name twice, because its a multiplayer game. But I want to press on the textbox and press my name on and then I will enter my name in the second box.

    I really don't understand much of this program. But i tried everything what i could.

    Would you give me an example or anything.

    Thank you!

  • edited October 2015 Answer ✓

    I am on a journey but can have a look later

    [EDITED]

    I posted now a bit...

    I hope this is not too long for you to grasp.

    It just shows the usage of states and how to get two player names

    anyway, it is all code from this forum, not by me really.

  • edited October 2015
    // demonstrates mouse usage with states.
    // one 2D array buttons holds all the screen buttons.
    // 2 user names can be entered.  
    
    // states:
    // consts (numbers must be unique)
    final int stateSplashScreen  = 0; // start screen
    final int stateName1         = 1;
    final int stateName2         = 2;
    final int stateGame          = 3;  
    final int stateGameOver      = 4;
    int state  = stateSplashScreen;       // current 
    final int stateMAX = stateGameOver+1; // the max number 
    
    // 2D array of buttons. 
    // First index = state, 2nd index = number of button in that state
    RectButton[][] buttons = new RectButton [stateMAX][3]; 
    
    // for the tool tip text 
    int timeSinceLastMouseMoved=0;
    
    // messages --------------------
    String msgText="";
    float msgX=0;
    float msgY=0;
    boolean msgShow=false;
    int msgFrameCount = 0;
    
    // text field 
    boolean textFieldCursorIsOn = true; // cursor on / off? 
    String enteredText = "";  // temp input text
    
    // player names 
    String namePlayer1 = "";  // results
    String namePlayer2 = "";
    
    // --------------------
    
    // colors
    final color colWhite      = color(255);
    final color colBlack      = color(0);
    final color colDarkGray   = color(111);
    //---
    final color colRed        = color(255, 0, 0);
    final color colGreen      = color(0, 255, 0);
    final color colBlue       = color(0, 0, 255);
    //---
    final color colYellow     = color(244, 244, 44);
    final color colDarkYellow = color(255, 200, 120);
    
    // colors for Buttons 
    final color col1 = #ff0000;
    final color col2 = #ffff00;
    final color col3 = #000000;
    
    
    // -------------------------------------------------
    
    void setup() {
      // init (runs only once)
      size(800, 800);
    
      // pre-init btns: set all to non-exist
      for (int i = 0; i<stateMAX; i++) {
        for (int i2 = 0; i2<buttons[i].length; i2++) {
          buttons[i][i2] =  new RectButton( 366, 3, 
          66, 70, col1, col2, false, "", "");
        }
      }
    
      // init btns
    
      // -------------
      // stateSplashScreen
      buttons[stateSplashScreen][0] =  new RectButton( 396 + 9, 433, 
      66, 70, col1, col2, true, "Next", "Go on, click me") ;
    
      // -------------
      // state name 1
      buttons[stateName1][0] =  new RectButton( 396 + 9, 433, 
      66, 70, col1, col2, true, "Next", "Go on, click me") ;
    
      // -------------
      // state name 2
      buttons[stateName2][0] =  new RectButton( 396 + 9, 433, 
      66, 70, col1, col2, true, "Next", "Go on, click me") ;
    
      // -------------
      // stateGame
      buttons[stateGame][0] =  new RectButton( 396 + 9, 433, 
      66, 70, col1, col2, true, "Hello", "Say Hello");
    
      buttons[stateGame][1] =  new RectButton( 396 + 9, 533, 
      66, 70, col1, col2, true, "Bye", "Say Bye");
    
      // -------------
      //stateGameOver
      buttons[stateGameOver][0] =  new RectButton( 396 + 9, 433, 
      66, 70, col1, col2, true, "Return", "return to game");
    } // func 
    
    void draw() { 
      // draw() runs on and on 
    
      getDataToDecideIfToolTipText();
    
      switch (state) {
    
      case stateSplashScreen:
        handleStateSplashScreen(); 
        break;
    
      case stateName1: 
        handleStateName1(); 
        break; 
    
      case stateName2: 
        handleStateName2(); 
        break; 
    
      case stateGame: 
        // Game
        handleStateGame();
        break; 
    
      case stateGameOver:
        // GameOver
        handleStateGameOver(); 
        break;
    
      default:
        // error
        println("Error number 939; unknown state : " 
          + state + ". Abort. #######################################");
        exit();
        break;
      } // switch
      //
    } // func 
    
    // ------------------------------------------------
    // functions for states - called from draw() 
    
    void handleStateSplashScreen() {
      background(11);
      fill(244, 3, 3); // red 
      text ("This is the Splash Screen....\n", 210, 313);
      showButtons();
    } // func 
    
    void handleStateName1() {
      background(11);
      fill(244, 3, 3); // red 
      text ("This is the Name state: PLAYER 1....\n", 210, 313);
      // draw the rest (the same for both states)
      handleInput();
    } // func 
    
    void handleStateName2() {
      background(11);
      fill(244, 3, 3); // red 
      text ("This is the Name state: PLAYER 2....\n", 210, 313);
      // draw the rest (the same for both states)
      handleInput();
    } // func 
    
    void handleInput() {
      // draw the rest 
      // (the same for both states) 
      //TextField
      fill(255);
      textSize(18);
      rect(25, 645, 200, 40);
      fill(0);
      text(enteredText+blinkChar(), 25+5, 645+30);
      fill(255, 2, 2, 11);
      toggleTextFieldCursorIsOn(); 
      // btns 
      showButtons();
    }
    
    void handleStateGame() {
      // Game
      background(11);
      fill(244, 3, 3); // red
      text (namePlayer1
        + " versus " 
        + namePlayer2
        + " <<<<<<<<<<<<<<<-------------", 
      17, 19);
    
      fill(244, 3, 3); // red 
      text ("This is the Game....\n"
        +" Don't touch the red rectangle...", 
      210, 313);
    
      noStroke();
      fill(255, 2, 2) ;
      rect(100, 100, 100, 100);
      fill(255, 2, 255) ;
      // rect(300, 100, 100, 100);
    
      if (mouseX > 100 && mouseX < 100 + 100  &&
        mouseY > 100   && mouseY < 100 + 100) {
        println ("collide");
        // change state
        timeSinceLastMouseMoved = millis();
        state = stateGameOver;
      }
    
      // btns
      showButtons();
    
      // message ----
      if (msgShow) {
        fill(colWhite);
        text (msgText, msgX, msgY); 
        if ( frameCount > msgFrameCount + 100 )
          msgShow=false;
      }//if
      //
    } // func 
    
    void handleStateGameOver() {
      // Game over / Lost 
      background(255);
      fill(244, 3, 3); // red 
      text ("You LOST....  ", 210, 313);
      text ("Hit any key to start Game", 210, 385);
      showButtons();
    } // func 
    
    // ----------------------------------------
    // input keyboard
    
    void keyPressed() {
    
      switch (state) {
    
      case stateSplashScreen: 
        // splash screen
        // do nothing 
        break; 
    
      case stateName1:
      case stateName2:
        // the same for both names
        //    if (keyCode>=32) 
        //      enteredText = enteredText + key;
        // SEE keyTyped() 
        break;
    
      case stateGame: 
        // Game
        // do nothing 
        break; 
    
      case stateGameOver:
        // Game Over
        timeSinceLastMouseMoved = millis();
        state = stateGame; 
        break;
    
      default:
        // error
        println("Error number 1039; unknown state : " 
          + state + ".");
        exit();
        break;
      } // switch
    } // func 
    
    void keyTyped() {
      // correct states? 
      if ( state == stateName1 || state == stateName2) {
        // yes 
        final char k = key;
    
        // quit?
        if (k == CODED)  
          return; // quit
    
        final int len = enteredText.length();
    
        if (k == BACKSPACE) {
          // shorten text   
          enteredText = enteredText.substring(0, max(0, len-1));
        } else if (len >= 44) { 
          return; // quit
        } else if (k == ENTER || k == RETURN) {
          // ignore
        } else if (k == TAB & len < 44-3) {
          // ignore 
          // tbox.txt += "    " ;
        } else if (k == DELETE) { 
          enteredText = ""; // reset
        } else if (k >= ' ') {  
          enteredText += str(k); // add this letter
        } // else if
      } // if
    }
    
    // ----------------------------------------
    // input mouse
    
    void mousePressed() {
    
      // reset timer for mouse over tool tip text  
      timeSinceLastMouseMoved = millis();
    
      // loop over all buttons (i) in the current state
      // when you add a new state, this func can stay the same 
      for (int i = 0; i<buttons[state].length; i++) {
        buttons[state][i].update();
        if (buttons[state][i].Exists && buttons[state][i].pressed()) {
          executeButton(state, i);
        }// if
      }//for
    }//func
    
    // ------------------------------------------ 
    // tools
    
    void showButtons() {
      // loop over all buttons (i) in the current state
      // when you add a new state, this func can stay the same 
      for (int i = 0; i<buttons[state].length; i++) {
        buttons[state][i].update();
        buttons[state][i].display();
        buttons[state][i].toolTip();
      } // for
    } // func 
    
    void executeButton(int stateLocal, int btnNumber) {
      // this is the command center for all states and all buttons : 
      // when you add a new state, you have to add something here.  
      switch(stateLocal) {
        // now the states: 
    
      case stateSplashScreen:
        // Splash Screen
        switch(btnNumber) {
        case 0:
          timeSinceLastMouseMoved = millis();
          state=stateName1;  // go on
          break; 
        default:
          // error
          println("Error number 1122; unknown button: " 
            + btnNumber
            + " in state: "
            + state 
            + ". Abort. #######################################");
          exit();
          break;
        } // inner switch
        break; 
    
      case stateName1:
        // only 1 btn
        namePlayer1 = enteredText;
        enteredText="";
        state=stateName2;   // go on
        break;
    
      case stateName2:
        // only 1 btn
        namePlayer2 = enteredText;
        enteredText="";
        state=stateGame;   // go on
        break;
    
      case stateGame:
        // game  
        switch(btnNumber) {
        case 0:
          println("Hello");
          msgOn("Hello ", 500, height-100);
          break; 
        case 1:
          println("Bye");
          msgOn("Bye ", 500, height-100);
          break; 
        default:
          // error
          println("Error number 1123; unknown button: " 
            + btnNumber
            + " in state: "
            + state 
            + ". Abort. #######################################");
          exit();
          break;
        } // inner switch
        break;
    
      case stateGameOver:
        // GameOver
        switch(btnNumber) {
        case 0:
          timeSinceLastMouseMoved = millis();
          state=stateGame; 
          break;    
        default:
          // error
          println("Error number 1124; unknown button: " 
            + btnNumber
            + " in state: "
            + state 
            + ". Abort. #######################################");
          exit();
          break;
        } // inner switch
        break;
    
      default:
        // error
        println("Error number 1339; unknown state : " 
          + state 
          + ". Abort. #######################################");
        exit();
        break;
        //------
      }// outer switch
    }//func
    
    void getDataToDecideIfToolTipText() {
      // get some data to decide if it is time to show tool tip text 
      if ((mouseX!=pmouseX) || (mouseY!=pmouseY)) {
        // mouse moved 
        // store time since last mouse moved 
        timeSinceLastMouseMoved = millis();
      }
    }// func
    
    void msgOn(String txt1, int x1, int y1) {
      msgText=txt1;
      msgX=x1;
      msgY=y1;
      msgShow=true;
      msgFrameCount = frameCount;
    }
    
    String blinkChar() {
      // returns a cursor or nothing 
      return (textFieldCursorIsOn) ? "|" : "";
    }
    
    void toggleTextFieldCursorIsOn() {
      // changes textFieldCursorIsOn 
      if (frameCount%30==0)
        textFieldCursorIsOn=!textFieldCursorIsOn;
    } 
    
    // ======================================
    // class RectButton and Button  
    
    class RectButton extends Button {
    
      // constr 
      public RectButton(int ix, int iy, 
      int isizeX, int isizeY, 
      color icolor, 
      color ihighlight, 
      boolean iExist, 
      String iText, 
      String iToolTipText 
        ) {
        x = ix;
        y = iy;
    
        sizeX = isizeX;
        sizeY = isizeY;    
    
        basecolor = icolor;
        highlightcolor = ihighlight;
        currentcolor = basecolor;
        Exists = iExist;
    
        Text = iText;
        ToolTipText = iToolTipText;
      } // constr 
    
      void display() {
        if (Exists) {
          stroke (ButtonStrokeColor); 
          strokeWeight(0); 
          fill(currentcolor, 30);
          rect(x, y, sizeX, sizeY);        
          if (Text != "") {
            //
            fill(0, 102, 153);
            textAlign(CENTER);
            textSize(16) ;
            text(Text, (x + (sizeX / 2.0)), (y + (sizeY / 2.0))+5);
            textAlign(LEFT);
          } // if (Text != "")
        } // if exists
      } // method display
    } // class
    
    class Button {
      int x, y;
      int sizeX;
      int sizeY;  
      color basecolor, highlightcolor;
      color currentcolor;
      boolean Exists  = false;  
      String Text     = "";
      String ToolTipText = ""; 
      //  String Tag = "";
      //  int Tag2 = 0;
      //  int TagMark = 0; 
      color ButtonStrokeColor = color (255, 255, 255);
    
      void update() {
        if (over()) {
          // Mouse over 
          currentcolor = highlightcolor;
        } else {
          // not Mouse over 
          currentcolor = basecolor;
        }
      } // update 
    
      void toolTip() {
        if (over()) {
          if (!ToolTipText.equals("")) {
            if (millis() - timeSinceLastMouseMoved > 800) {
              fill(colYellow);
              noStroke();
              textSize (12);
              float tw = textWidth(ToolTipText);
              float xLeft = mouseX-(tw/2); 
              float xRight = mouseX-(tw/2) + tw; 
              float xLeftText = mouseX; 
              if (xRight>=width) { 
                xLeft= width-tw-2;
                xLeftText= xLeft + tw/2;
              } 
              if (xLeft< 2) 
              { 
                xLeft=2;
                xLeftText= 2 + tw / 2;
              }
              rect (xLeft, mouseY+32, tw+4, 20);
              textAlign(CENTER);
              fill(0);
              text(ToolTipText, xLeftText, mouseY+44);
              textAlign(LEFT);
              textSize(16);
            } //  if
          } // if
        } // if
      } // func 
    
      boolean pressed() {
        if (over()) {
          return true;
        } else {
          return false;
        }
      }  // pressed; 
    
      boolean over() {
        if (overRect(x, y, sizeX, sizeY) ) {
          return true;
        } else {
          return false;
        }
      } // over 
    
      boolean overRect(int x, int y, 
      int width, int height) {
        if (mouseX >= x && mouseX <= x+width &&
          mouseY >= y && mouseY <= y+height) {
          return true;
        } else {
          return false;
        }
      } // overRect
      //
    } // class
    //
    
  • I hope this is not too long for you to grasp.

    It just shows the usage of states and how to get two player names

    anyway it is all code from this forum.

  • OK, you need to learn how to format code here on the forum

    you could go back and edit your post to format the code better

    you need to learn how to give your posts better headlines. Don't use capitals and don't say you need help, just describe your issue and post code.

    a memory game

    to do a memoy game is a little harder.

    Are you familiar with object oriented programming? There is a tutorial for that. It's called Objects.

    Thus each card would have a x,y position and an image and an ID that it shairs with its partner.

    You would have an array of those objects.

  • I am on a longer journey now, so I can't help you with that

Sign In or Register to comment.