Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (534)
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  
  Listen to multiple KeyEvents at once?  (Read 1089 times)
0 Members and 1 Guest are viewing this topic.
Offline Ivoos

Junior Newbie





« Posted 2008-06-06 19:59:46 »

Last week I had a problem with a small Asteroids clone I was making, I couldn't shoot while moving and couldn't move to 2 sides at once...
After a lot of googling and trial and error my KeyListener now looks 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  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
public class ArrowListener implements KeyListener{

    private PlayerModel model;
   
    private boolean left = false;
    private boolean right = false;
    private boolean up = false;
    private boolean down = false;
    private boolean space = false;
   
   
    public ArrowListener(PlayerModel model){
        this.model = model;
    }

    public void keyPressed(KeyEvent e) {
        setKey(e.getKeyCode(), true);
        doSomething();
    }

    public void keyTyped(final KeyEvent e) {
    // empty
   }

    public void keyReleased(KeyEvent e) {
        setKey(e.getKeyCode(), false);
        doSomething();
    }

    public void setKey(int key, boolean pressed){
        if (key == KeyEvent.VK_LEFT) {
            left = pressed;
        } else if (key == KeyEvent.VK_RIGHT) {
            right = pressed;
        } else if (key == KeyEvent.VK_UP) {
            up = pressed;
        } else if (key == KeyEvent.VK_DOWN) {
            down = pressed;
        } else if (key == KeyEvent.VK_SPACE) {
            space = pressed;
        }
    }

    public void doSomething() {
        if (left == true) {
            model.left();
        } if (right == true) {
            model.right();
        } if (up == true) {
            model.up();
        } if (down == true) {
            model.down();
        } if (space == true) {
            model.shoot();
        }
    }
}


Is there anyway to do this any better?
Right now, it's not 100% right, when you're pressing space and left at once, and let go of the left arrow you stop shooting too...
Offline darkprophet

Senior Member




Go Go Gadget Arms


« Reply #1 - Posted 2008-06-06 20:11:44 »

Yes, there is a better way of doing it:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
private byte[] keyCodes = new byte[256];

public void keyPressed(KeyEvent e) {
        keyCodes[e.getKeyCode()] = true;
}

public void keyReleased(KeyEvent e) {
        keyCodes[e.getKeyCode()] = false;
}

public void update() {
        if (keyCodes[KeyEvent.VK_LEFT] && keyCodes[KeyEvent.KEY_UP) {
                 x-=1; y-=1;
        }
}


You shouldn't do the logic on the AWT thread which the listener runs in...update should just run in a loop on the main thread.

PS. Thats pseudocode i wrote in the forum, not sure if it will work or not, dont take it verbatim, but the spirit is there.

DP Smiley

Friends don't let friends make MMORPGs.

Blog | Volatile-Engine
Offline Ivoos

Junior Newbie





« Reply #2 - Posted 2008-06-06 20:44:12 »

Thanks a lot for your reply, it works great now, except for 1 minor thing, The left key doesn't seem to work as good as the right key, very weird...
If I press up, right and spacebar at the same time, it goes to right and forward and it shoots. But if I let go of the right arrow and press to the left, nothing happens. The other way around it works fine  Huh 

Anyway, here's the game: http://alexandria61.2mhost.com/~mosselst/website/ivo/asteroids.zip
Keep in mind I'm a noob when it comes to java and that I made it in 2-3 hours (not including the time I spent googling the arrow-problem :p )
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.

pw (35 views)
2014-07-24 01:59:36

Riven (33 views)
2014-07-23 21:16:32

Riven (21 views)
2014-07-23 21:07:15

Riven (24 views)
2014-07-23 20:56:16

ctomni231 (55 views)
2014-07-18 06:55:21

Zero Volt (47 views)
2014-07-17 23:47:54

danieldean (38 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!