Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How'd you implement an action not being repeated when the button is held down?  (Read 917 times)
0 Members and 1 Guest are viewing this topic.
Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Posted 2013-05-29 09:16:43 »

I'm pretty sure I can find my own way around this, but I was curious to learn how you folks do it/would do it.

Like for example:

(1) I press the jump button
(2) My character jumps
(3) I just hold the jump button down after pressing it
(4) My character falls to the ground with me still holding the jump button

If you don't want the character to jump again if the player is still holding down the button that made the character jump, how would you do it?

Same thing like shooting for example, how to you prevent continuous shooting when the shoot button is held and instead require the player to press the button repeatedly instead?

Offline UprightPath
« Reply #1 - Posted 2013-05-29 09:29:15 »

It depends! Check if the library you have provides the functionality you need. Some of them do follow when a key was first pressed. That's what you're looking for.

If it doesn't, then there are several ways to do it. If it's an event driven system you'll get an event that says "Key was pressed" which you can react to. (The merits of using event driven input in games is another discussion. Cheesy)

If it's a polling system (You ask whether it's up or down and it tells you) then you have your own key object that would keep track of the button's state. The object would have: isButtonDown() and isFirstDown(), or something like that. The first one returns true if the button is currently down. The second will only return true during the time step in which it first went from being up to being down. How you do this internally is up to you. Since you'll typically be polling the keys EVERY time step, I'd do something like the code that follows. Yes, you end up setting firstDown a BUNCH of times when you might not need to, but meh.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
class GameButton {
   // Members used to keep track of key information for KEY IS PRESSED check
  private boolean down;
   private boolean firstDown;
   public void pollButton() {
      if( KEY IS PRESSED) {
         if(!down) {
            down = true;
            firstDown = true;
         } else {
            firstDown = false;
         }
      } else {
         down = false;
      }
   }
   
   public boolean isDown() {
      return down;
   }
   
   public boolean isFirstDown() {
      return firstDown;
   }
}

Offline Gamerulf

Junior Member


Medals: 2



« Reply #2 - Posted 2013-05-29 09:32:08 »

You could have a boolean. jumpReady.
That's set to true when key is released and you are on the ground and false when you are in the air and key is pressed.

Depends on what game you are doing though.

- Gamerulf
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline StrideColossus
« Reply #3 - Posted 2013-05-29 09:33:12 »

You don't mention what technologies and frameworks you are using, but not too worry the approach is generally the same.

Basically you are talking about game state, i.e. whether the character is jumping or not in your scenario.

Presumably you have some sort of callback handler that starts the jump when you press the button, and possible some sort of listener that knows when the jump animation has completed?  You'll need to check whether you're already jumping, plus you need some means of letting it know when the jump has ended, i.e. something along these lines (in pseudo code):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
handler() {
  if( !jumping ) {
    start jump animation // calls animation-listener() when it's finished
   jumping = true;
  }
  // otherwise ignore keypress
}

animation-listener() {
  jumping = false;
}


The jumping boolean is the game state.

The above is very crude but hopefully you get the idea.

Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Reply #4 - Posted 2013-05-29 09:39:21 »

Sorry, I'm using JInput, but I'm interested in the different approaches nonetheless Smiley Thanks.

Offline Oskuro

JGO Knight


Medals: 39
Exp: 6 years


Coding in Style


« Reply #5 - Posted 2013-05-29 13:35:36 »

Maybe these can help?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
public class MyListener implements java.awt.event.KeyListener
{
   //...

   @Override
   public void keyPressed( KeyEvent keyEvent )
   {
      // What to do when the key is pressed
  }

   @Override
   public void keyReleased( KeyEvent keyEvent )
   {
      // What to do when the key is released
  }

   @Override
   public void keyTyped( KeyEvent keyEvent )
   {
      // I'm still not clear on the point of this one
  }

   //...
}


In my code, I have the character start moving upon a keydown event, and stop moving on a keyup.

From what I've seen, more advanced input managers also have a timer indicating how long the key has been pressed, or how long since it was pressed.

Offline relminator
« Reply #6 - Posted 2013-05-29 15:38:56 »

Poll your input and store it in a separate boolean or int array then do som xor magic.
Offline Fsig

Senior Newbie





« Reply #7 - Posted 2013-06-25 23:41:12 »

I stored the pressed key's in a array.
Then I created a method to release the key from the array.

So there is a simple boolean check to see if a key is down (in the array).

So check if your key is down, if it is do the action, after the action, release the key.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (43 views)
2014-09-23 14:38:19

radar3301 (25 views)
2014-09-21 23:33:17

BurntPizza (62 views)
2014-09-21 02:42:18

BurntPizza (32 views)
2014-09-21 01:30:30

moogie (37 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!