3 Replies Latest reply: Dec 24, 2009 7:30 AM by 843853 RSS

    Collision detection problem

    843853
      I am trying to create a spelling game. The user guides a circle around the screen using the arrow keys. A word is displayed at the bottom of the screen. The letters are displayed separately as tiles and the user has to move around and collect them in order. Once a letter is 'collected' it disappears.
      The problem I have is that I can't get it to work for double or repeated letters. I have tried all sorts of things and I just can't make it work consistently.
      One problem is that the letters don't seem to be removed immediately and so if the user continues to move over space that was occupied by a letter that was just collected, and the word has a double letter e.g. 'meet', then it registers that the letter has been collected again without removing anything from the board. The label becomes invisible, but collisions with it are still detected. I have no idea why this would be the case. (Is there any way to get a component, in this case a JLabel, to disappear immediately.)
      The code is below. I would appreciate any ideas.
      void checkCollision(int code) {
              Rectangle playerRec = new Rectangle(player.getPlayerXPos(),
      player.getPlayerYPos(), 10, 10);
              // Check for collision with a letter
              for (int i = lettNo; i < wordLeng[wordNo]; i++) {
                  Rectangle rec = new Rectangle(lettLabel.getX(),
      lettLabel[i].getY(), 30, 30);
      if (rec.intersects(playerRec)) {
      String let = lettLabel[i].getText();
      String let2 = individualLet[wordNo][lettNo];
      if (let.equals(let2)) {
      remove(lettLabel[i]);
      if (previousLetter.equals(let2)) {
      remove(lettLabel[i]);
      }
      previousLetter = let2;

      repaint();
      lettNo++;
      if (lettNo == wordLeng[wordNo]) {
      wordNo++;
      if (wordNo < 20) {
      wordL.setWord(wordsToUse[wordNo]);
      wordL.repaint();
      player.setStartPosition();

      lettNo = 0;
      l = whichLet(individualLet, wordNo);
      assignLet(l);
      repaint();
      } else {
      JOptionPane.showMessageDialog(this, "You have won." +
      " The password for the next level is "+ password[level]);
      }
      }
      } else {
      stopMovement(code);
      System.out.println("That is the wrong letter.");
      }
      }
      }

      }