Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Concurrent Keyboard Input in Applets
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
jas256




PostPosted: Fri May 11, 2007 6:42 pm   Post subject: Concurrent Keyboard Input in Applets

Hello everybody! I am currently making a two-player single-machined Pong applet (I would use networking, but that's a bit too complex for this assignment). I am using the two arrow keys for one user, and the W/S keys for the second user. Now, this movement all works when I move each paddle individually. However, the moment that I try and move both paddles at the same time, one of the user's input will be ignored.

This is, I assume, because a KeyEvent is only created and the keyPressed method called only for the most recent key press.

This is obviously an issue because one can use it to cheat. (For example, by pressing down on your controls, the opponent can no longer move).

I have considered other alternatives, such as making a new Thread (but I don't think this will work since it must implement the KeyListener interface), requiring users to press and release movement (by using the keyTyped method instead), or by ignoring key movement if the ball is not currently coming towards that player.

I was just wondering if anyone had any other ideas of how this could be done. Now, I do not want somebody to write the code for me since I actually want to learn about new things, instead of just passing this assignment. A description of a system that will work, or a link to a tutorial or other reference will be great!

(PS, I have tried searching these forums but I have as yet come up with nothing dealing with this issue, and our class currently lacks an on-site teacher.)

Here is the code for the keyPressed method, and keyTyped and keyReleased are blank.

code:
    public void keyPressed(KeyEvent e) {
        // The boolean keyDown() method is deprecated, use this one
        int key = e.getKeyCode();
        if (key == KeyEvent.VK_W) {
            paddles[0].incrementY(-5);
        } else if (key == KeyEvent.VK_S) {
            paddles[0].incrementY(5);
        } else if (key == KeyEvent.VK_UP) {
            paddles[1].incrementY(-5);
        } else if (key == KeyEvent.VK_DOWN) {
            paddles[1].incrementY(5);
        }
    }
Sponsor
Sponsor
Sponsor
sponsor
OneOffDriveByPoster




PostPosted: Sat May 12, 2007 9:10 am   Post subject: Re: Concurrent Keyboard Input in Applets

jas256 @ Fri May 11, 2007 6:42 pm wrote:
This is, I assume, because a KeyEvent is only created and the keyPressed method called only for the most recent key press.
Well it is something like that. Why not print something in the method to see?

Quote:
I was just wondering if anyone had any other ideas of how this could be done. Now, I do not want somebody to write the code for me since I actually want to learn about new things, instead of just passing this assignment. A description of a system that will work, or a link to a tutorial or other reference will be great!
You know that you will get a call to keyPressed for at least the user's first press of the key, and you know that you will get a call to keyReleased only when the user releases the key. So if you respond to both the keyPressed and keyReleased, what can you do?
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: