×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    11

    Using Keypressed, keyreleased , and keyrepeated

    Hi,

    Woking on a game and have got the keypressed working just not the other two. Have looked at different ways of doing it on various sites, cant see what i am doing wrong. any hlep will be appreciated...

    public void keyReleased(int keyCode)
    {
    repaint();
    }

    public void keyRepeated(int keyCode) //activates when the user repeats any button
    {
    try
    {
    eventType = "repeated";
    this.keyCode = keyCode;
    while(this.getGameAction(keyCode)== keyCode)
    {
    keyPressed(keyCode);
    repaint();

    }
    }
    catch(Exception e)
    {
    System.out.println("Sprite Control Class : keyRepeated :" + e);
    }
    }
    public void keyPressed(int keyCode) // this function is activited when the user hits a button on the keypad
    {
    try
    {
    eventType = "pressed"; // set event type
    this.keyCode = keyCode; // set keycode
    if ( (50 == keyCode || 52 == keyCode || 54 == keyCode || 56 == keyCode) &&running == true)
    {
    PlayerShip1.moveShip(keyCode); // if the keycode is 50 or 52 or 54 or 56 and game is running
    repaint(); // call the player ship class to update and repaint
    }
    if (53 == keyCode && running == true) // if the keycode is 53 and game is running
    {
    shipPositionX = PlayerShip1.getPositionX(); // set positions
    shipPositionY = PlayerShip1.getPositionY();
    int num1 = playerImage.getWidth(); // set width of image
    PlayerShoot1.Shoot((num1 + shipPositionX), (shipPositionY+5), 0); // call player shoot to add shot at the positions and also the bullet type
    repaint();
    }
    if (51 == keyCode && running == true)
    {
    shipPositionX = PlayerShip1.getPositionX();
    shipPositionY = PlayerShip1.getPositionY(); // same as last but bullet type
    int num1 = playerImage.getWidth(); // is 1 for missile
    PlayerShoot1.Shoot(num1 + shipPositionX, shipPositionY, 1);
    repaint();
    }
    }
    catch(Exception e)
    {
    System.out.println("Sprite Control Class : keypressed :" + e);
    }
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    11
    Just testing it keyrepeated isnt being called at all in the program, keyreleased is though.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Location
    Romania
    Posts
    10
    Your keyRepeated function is a mess.
    First of all, don't do while's in event-handling functions, as they are called as a response to an event, and you are just blocking it.
    Second,

    while(this.getGameAction(keyCode)== keyCode)

    is wrong. Don't expect getGameAction(keyCode) to be the same as keyCode.

    My advise is that on keyRepeated, just call keyPressed with the suplied keyCode.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    11
    I have updated the code, keyRepeated is still not being called

    Code:
    public void keyReleased()
       {
          buttonPressed = false;
          repaint();
         
       }
    
       public void keyRepeated(int keyCode) //activates when the user repeats any button
       {
    
          try
          {
             buttonPressed = true;
             eventType = "repeated";
             this.keyCode = keyCode;
    
             if (hasRepeatEvents() && buttonPressed == true)
             {
                keyPressed(keyCode);
                repaint();
             }
          }
          catch(Exception e)
          {
             System.out.println("Sprite Control Class : keyRepeated :" + e);
          }
       }
       public void keyPressed(int keyCode)   // this function is activited when the user hits a button on the keypad
       {
          try
          {
             eventType = "pressed";              // set event type
             buttonPressed = true;
             this.keyCode = keyCode;             // set keycode
             if( (50 == keyCode || 52 == keyCode || 54 == keyCode || 56 == keyCode) &&running == true && buttonPressed == true)
             {
                PlayerShip1.moveShip(keyCode);             // if the keycode is 50 or 52 or 54 or 56 and game is running
                repaint();                                 // call the player ship class to update and repaint
             }
             if (53 == keyCode && running == true && buttonPressed == true)         // if the keycode is 53 and game is running
             {
                shipPositionX = PlayerShip1.getPositionX();     // set positions
                shipPositionY = PlayerShip1.getPositionY();
                int num1 = playerImage.getWidth();              // set width of image
                PlayerShoot1.Shoot((num1 + shipPositionX), (shipPositionY+5), 0);  // call player shoot to add shot at the positions and also the bullet type
                repaint();
             }
             if (51 == keyCode && running == true)
             {
                shipPositionX = PlayerShip1.getPositionX();
                shipPositionY = PlayerShip1.getPositionY();           // same as last but bullet type
                int num1 = playerImage.getWidth();                    // is 1 for missile
                PlayerShoot1.Shoot(num1 + shipPositionX, shipPositionY, 1);
                repaint();
             }
            else if(keyCode == 55)
             {
                keyReleased();
             }
          }
          catch(Exception e)
          {
             System.out.println("Sprite Control Class : keypressed :" + e);
          }
       }

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    11
    since keyrepeated and keyreleased dont seem to be called im checking in the game loop if a button is pressed and calling the keyrepeated, it works as in the ship moves but doesnt stop moving when i stop pressing the button only stops when i push another button.

    Code:
      if((50 == keyCode || 52 == keyCode || 54 == keyCode || 56 == keyCode) && keyCode != 53)
                {
                   repeatKeys();
                }
                else if(???????)
                {
                      repaint();
                }

  6. #6
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395
    Not all devices call keyRepeated(). Use only keyPressed() and keyReleased().

    Also, *never* perform any of your game logic in event handers like keyPressed() or paint(). Avoid calling repaint() (and *never* call serviceRepaints()!) from other event methods like keyPressed().

    Create a single, separate Thread for game logic. Use your key methods only to record what keys are pressed. Then pick up this information in the game loop, process it, and call repaint()/serviceRepaints().

    Graham.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •