You Are Here:

Community: Developer Discussion Boards

#1 Old Using Keypressed, keyreleased , and keyrepeated - 2005-04-28, 14:33

Join Date: Jan 2005
Posts: 11
davewit13
Offline
Registered User
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);
}
}
Reply With Quote

#2 Old 2005-04-29, 14:55

Join Date: Jan 2005
Posts: 11
davewit13
Offline
Registered User
Just testing it keyrepeated isnt being called at all in the program, keyreleased is though.
Reply With Quote

#3 Old 2005-04-29, 17:48

Join Date: Jun 2004
Posts: 10
Location: Romania
mishou
Offline
Registered User
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.
Reply With Quote

#4 Old 2005-04-30, 10:51

Join Date: Jan 2005
Posts: 11
davewit13
Offline
Registered User
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);
      }
   }
Reply With Quote

#5 Old 2005-04-30, 11:51

Join Date: Jan 2005
Posts: 11
davewit13
Offline
Registered User
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();
            }
Reply With Quote

#6 Old 2005-05-14, 14:12

Join Date: Jun 2003
Posts: 4,322
Location: Cheshire, UK
grahamhughes's Avatar
grahamhughes
Offline
Forum Nokia Champion
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.
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

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

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia