2 Replies Latest reply on Apr 6, 2010 1:35 PM by 843853

    Getting GameCanvas KeyStates

      Hi guys,
      im writing a simple checkers game using t GameCanvas.
      i have set up a checkers board and use a cursor to navigate the board to select sprites and etc..
      I am using getKeyStates() function to get input. The problem is multiple inputs of FIRE_PRESSED which makes the cursor select sprites multiple times and makes the program behave weirdly.
      I have seen keyPressed method but i donot know how to use that in my code.
      Pliz help guys.

      public class CheckersGameCanvas extends GameCanvas implements Runnable {

      //declare private variables

      public CheckersGameCanvas() {

      //overide superclass

      private void init() throws IOException {
      //initialize variables

      public void run() {
           int iKey = 0, c, r, x, y;
           String turn = "blue";
           g = getGraphics();
           boolean isSelected = true;
           while (isRunning) {
           iKey = getKeyStates();     //heres my problem (gets multiple inputs)
           if ((iKey & FIRE_PRESSED) != 0) {                    //which causes
                x = sPieceCursor.getRefPixelX(); //these values
                y = sPieceCursor.getRefPixelY(); //these values
                c = x / 16; //these values
                r = y / 16; //these values
                //if not a piece layer-->warn
                isSelected = true;
                if (turn == "blue") {
                tlGreyGrid.setCell(c, r, 0);
                     while (isSelected) {
                     iKey = getKeyStates(); //this doesnt even get the chance to execute :(
                     if ((iKey & FIRE_PRESSED) != 0) {

                          int nx = sPieceCursor.getX(); //and these values to be same
                          int ny = sPieceCursor.getY(); //and these values to be same
                          int nc = nx / 16; //and these values to be same
                          int nr = ny / 16; //and these values to be same

      I have also tried using thread.sleep but thats unreliable also.
      Pliz how to use keyPressed(int) here?
        • 1. Re: Getting GameCanvas KeyStates
          You have several problems with this, the one that is being express is just a visible manifestation of them.

          1 - you cannot lock your game into an infinite loop and expect it to function well. You need a basic game loop setup with event listeners and timer driven.

          2 - you don't allow other events to process normally--see problem 1.

          3 - animation/graphics is going to be flickery and sluggish--see problem 1.

          4 - you have great difficulty if not an impossibility go get meaningful feedback from any components--see problem 2.


          There are a few of the things that you may now or you will see very shortly. Look up in the Tutorial on how to use javax.swing.Timer to setup scheduling control cycles for your games and animations.
          • 2. Re: Getting GameCanvas KeyStates
            Implementing a timer for input threads seems like i will have to start again on a new canvas.
            Anyways i got it to run using keyPressed(). Thanks anyways mate.