4 Replies Latest reply: Feb 19, 2010 8:57 AM by 843853 RSS

    Spelling game problem

    843853
      I have a problem that I have been trying to solve for some weeks without success. I hope that someone can help me.
      I am trying to produce a game where children use arrow keys to move around the screen to collect letters to spell out words. For each 'turn' a word is displayed and the child has to move around and run into the correct letters in order. The letters are randomly placed in one of fifteen positions on the screen. When a word has been completed a new word is displayed. At the end of a level a dialog appears showing the score, bonuses, etc. (There are enemies, obstacles and time bonuses, but these aren't strictly relevant because they seem to work fine.)
      My problem is that if the last letter position is the same as a letter position for the next word, a collision is detected and obviously it is a collision with the wrong letter. For example, if the last letter of a word was 'e' and it was at 100, 200, if the letter 'f' (not the first letter of the next word) is also placed at 100, 200 a collision is detected.
      I have tried moving the player back five pixels when a collision is detected. I have tried 'turning off' registering key events (so the player doesn't move even though the key is being held down). I have tried inserting a modal dialog to stop the problem. I am obviously thinking about the problem wrongly and would love some ideas on a way forward.
      I have included a small section of my code below. The first section is part of the collision detection code.
      for (int i = 0; i < wordLeng[wordNo]; i++) {
                  Rectangle rec = new Rectangle(letterPoint, new Dimension(30, 30));
      if (rec.intersects(playerRec)) {
      String let = lettLabel[i].getText();
      String let2 = individualLet[wordNo][lettNo];
      if (let.equals(let2)) {
      letterPoint[i].x = -50;// These two lines are needed to stop problems with double letters.
      letterPoint[i].y = -50;
      this.remove(lettLabel[i]);
      validate();
      score = score + 10;
      repaint();
      stopMovement(code); // to stop it affecting the next level
      lettNo++;
      if (lettNo == wordLeng[wordNo]) {
      nextWord = new NextWord(null, true);
      stopMovement(code); // to stop it affecting the next level
      player.setStartPosition();
      wordNo++;
      moveCount = 0;
      if (wordNo < 3) { // This could be twenty as there are twenty in each list.
      wordL.repaint();
      nextWord.setWord(wordsToUse[wordNo]);
      nextWord.setVisible(true);
      wordL.setWord(wordsToUse[wordNo]);
      lettNo = 0;
      l = whichLet(individualLet, wordNo);
      assignLet(l);
      repaint();
      } else {..........


      void stopMovement(int c) {
      // Stops the player from moving.
      int code = c;
      if (code == KeyEvent.VK_LEFT) {
      player.setXChange(5);
      }
      if (code == KeyEvent.VK_RIGHT) {
      player.setXChange(-5);
      }
      if (code == KeyEvent.VK_DOWN) {
      player.setYChange(-5);
      }
      if (code == KeyEvent.VK_UP) {
      player.setYChange(5);
      }
      }
      Any ideas would be greatly appreciated.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
        • 1. Re: Spelling game problem
          843853
          Once you complete the current challenge, then you need to go out of that scope and reinitialize the new screen with the new word and other objects.
          • 2. Re: Spelling game problem
            843853
            Thank you for trying to help. I am not sure what 'go out of that scope' means. What the program does is check if there are any more words and if there are display the next one. The problem is that before the next set of letters is displayed a collision with one of the new letters is detected because the user keeps his/her finger on an arrow key.
            I tried stopping the player moving by adding an if clause to the key listener -- effectively listening was turned off until the screen was redrawn and then turned on again -- but it didn't solve the problem.
            • 3. Re: Spelling game problem
              843853
              From your code example, I don't see how your stop movement does what you think: you still have a movement rate assigned in it. To stop movement by setting a flag in your listener so it will just return without any event being processed.

              You have some basic comparisons, do you reset those--I don't see where you do, so if you conditionals have been met already, and you don't change them, you always have a true.

              What I mean by you have to go out of the context is: where ever you initialize those variables, you have to go back there and reinitialize everything--except the scoring--if you don't you, at best, have an error condition waiting to happen (as you can see you do).

              The problem in your code is only marginally, if at all, contained in the code you offer for analysis--open up your debugger and step through and you should get a much better idea of what is happening and how to solve it. You know the end result, but as of yet, you haven't a clue why it's happening--use your debugger and you'll see what I am saying.
              • 4. Re: Spelling game problem
                843853
                Thank you for you comments. After reading them I tried some other ideas which didn't solve the problem. Then my twelve year old son looked over my shoulder and said, "Why don't you just have the tiles start in different places?" The problem is now solved. Thanks again for taking the time to reply.