Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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  
  Holding down keys  (Read 3955 times)
0 Members and 1 Guest are viewing this topic.
Offline wookoouk

Senior Newbie





« Posted 2012-02-27 19:35:01 »

I have an issue with the way I am having the player shoot.

I am aiming for when W,A,S,D are pressed the player shoots ONCE in that direction but with the way I have written it, there are multipul bullets fireing and the user is able to hold down the keys for rapid fire (unwanted), Im sure it could be done via bools but I dont know if there is a better way of wording the below text to get what I am after.


if (Keyboard.isKeyDown(Keyboard.KEY_W)){
         shootUp();
}

any ideas?
Offline RylandAlmanza

Junior Duke


Medals: 3



« Reply #1 - Posted 2012-02-27 19:41:43 »

I think this will work better:
1  
2  
3  
4  
5  
6  
7  
while (Keyboard.next()) {
   if (Keyboard.getEventKeyState()) {
      if (Keyboard.getEventKey() == Keyboard.KEY_W) {
         shootUp();
      }
   }
}
Offline wookoouk

Senior Newbie





« Reply #2 - Posted 2012-02-27 19:43:42 »

sweet, will give it a try now, feed my son and update.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline wookoouk

Senior Newbie





« Reply #3 - Posted 2012-02-27 19:45:28 »

my son can wait  Grin,

Works perfectly thanks chap.
Offline RylandAlmanza

Junior Duke


Medals: 3



« Reply #4 - Posted 2012-02-27 19:47:35 »

No problem. Smiley I think I just found an optimization for it, too. You might want to change it to this:
1  
2  
3  
4  
5  
6  
7  
while (Keyboard.next()) {
   if (Keyboard.getEventKey() == Keyboard.KEY_W) {
      if (Keyboard.getEventKeyState()) {
         shootUp();
      }
   }
}

Let me know if you need me to explain what it does. Smiley
Offline Nyhm

Senior Duke


Medals: 3
Projects: 1


Island Forge


« Reply #5 - Posted 2012-02-27 20:38:27 »

Does LWJGL provide a more reliable/consistent keypress API than AWT/Swing?

I ask because I've experienced different keyboard responses on different platforms, requiring hokey timing tricks. Particularly, Linux implements key-repeat by firing a sequence of key up/down signals, which Java/AWT/Swing happily reports to the application. In Windows, key repeat is handled at the app level, so only one key-down *time passes* key-up sequence is reported to Java. Sadly, the Windows signals are much better to program against. (For the record, my response to these issues using raw AWT/Swing API was to throw out keyboard control).

Island Forge: Create Islands with Stories for Others to Explore!
Free-to-Play with Membership and Upgrade options!
Offline RylandAlmanza

Junior Duke


Medals: 3



« Reply #6 - Posted 2012-02-27 20:41:31 »

Particularly, Linux implements key-repeat by firing a sequence of key up/down signals, which Java/AWT/Swing happily reports to the application. In Windows, key repeat is handled at the app level, so only one key-down *time passes* key-up sequence is reported to Java.
Ah, so my solution may only work correctly on windows, then?
Offline Nyhm

Senior Duke


Medals: 3
Projects: 1


Island Forge


« Reply #7 - Posted 2012-02-27 20:46:38 »

Particularly, Linux implements key-repeat by firing a sequence of key up/down signals, which Java/AWT/Swing happily reports to the application. In Windows, key repeat is handled at the app level, so only one key-down *time passes* key-up sequence is reported to Java.
Ah, so my solution may only work correctly on windows, then?

Not sure - I assume you're referencing LWJGL API, as Java/Swing doesn't have a Keyboard class (unless that's Java 7). I do not have experience with LWJGL. My account was referring to Java 6 AWT keyboard events. Am I confused/confusing?

Basically, I'm wondering how to best handle key-down events in Java, as I haven't found a (good) way with AWT/Swing.

Island Forge: Create Islands with Stories for Others to Explore!
Free-to-Play with Membership and Upgrade options!
Offline UprightPath
« Reply #8 - Posted 2012-02-27 20:47:26 »

Ah, yes and no. It's probably best if you write your own key managed so that you can really manage how it works. Especially with the key repeat stuff some computers have.

I use something like this:
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  
26  
27  
28  
29  
public enum KeyCommand {

    SHOOT(10);
    private final int repeat;
    private int countDown;
    private boolean armed;

    KeyCommand(int repeat) {
        this.repeat = repeat;
    }

    public void updatePressed(boolean pressed) {
        countDown--;
        if (pressed) {
            if (countDown < 0) {
                armed = true;
                countDown = repeat;
            }
        }
    }

    public boolean isArmed() {
        return armed;
    }

    public void resetArmed() {
        armed = false;
    }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
KeyCommand.SHOOT.Input.isKeyDown(SHOOT_KEY));

if(KeyCommand.SHOOT.isArmed()) {
//shoot code
KeyCommand.SHOOT.resetArmed();
}
[code]

The int value to the enum constructor sets how many frames can pass before another keypress/instance of the key being down, causes it to be 'armed' again. So that if you're mashing the key or you're holding it down or you're pressing when you're ready, it shouldn't allow you to have a beam-spam.
[/code]

Offline wookoouk

Senior Newbie





« Reply #9 - Posted 2012-04-17 10:35:33 »

I only just noticed these posts, sorry. Thanks for the info guys, I will implement this tonight. You are a great community.
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.

Longarmx (41 views)
2014-10-17 03:59:02

Norakomi (33 views)
2014-10-16 15:22:06

Norakomi (26 views)
2014-10-16 15:20:20

lcass (30 views)
2014-10-15 16:18:58

TehJavaDev (59 views)
2014-10-14 00:39:48

TehJavaDev (60 views)
2014-10-14 00:35:47

TehJavaDev (50 views)
2014-10-14 00:32:37

BurntPizza (66 views)
2014-10-11 23:24:42

BurntPizza (38 views)
2014-10-11 23:10:45

BurntPizza (80 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!