Ok, I posted something similar to this in "New to Java," but I didn't quite get the response I wanted (instead I learned a different neat trick).
In a multiplayer game (in a Java Frame), how do I get the computer to recognize two KeyEvents at the same time? Two objects can't move at the same time normally. Also, if one player holds down a key, the autorepeat will prevent the other player from moving (unless they release their key and press it again, which will stop the other player, and so on). Does anyone know how to solve this? Any help would be much appreciated.
Thanks,
rogueace
12-07-2004, 04:45 AM
ractoc
You could assign each player his own Thread with respect to event handling. Then all you need to do is let that Thread use callback methods to set the needed variables to have their object move over the screen.
The main Thread (the one with the frame in it) then only has to redraw every x seconds. Because the player threads can update their object coordinates through callback methods the repaint will automatically put the objects at the new location.
12-07-2004, 06:19 AM
mikeBarr81
You have to ignore the auto-repeats. The KeyEvent recognises 3 different states for a key which I think are called KeyPressed, KeyReleased and KeyTyped (pressed and then released). You need to listen on the KeyPressed and KeyReleased states.
When the relevant key is pressed you set a boolean flag in the object to true (ie moveForward = true; or something). When that same key is released you set it to false. The auto-repeat will now have no effect because if the same key is reported as pressed again nothing will happen, as the flag is already true. You can then use the boolean flags to determine what to do each time you redraw.
12-07-2004, 04:58 PM
rogueace
I know how to stop the auto-repeat (using getWhen()).
How do I assign each player his own Thread (sample code, please)?
I'm fairly new to Java (taking a class and I'm about 4 months in), and I'm pretty bored in here at this level (i'm in the class right now actually, typing this), so any help would be much appreciated so that I can move faster.
12-10-2004, 04:05 AM
sjalle
If you have a class like 'Player' then that class has to implement the runnable interface, e.g. it must have a run method and a start and stop method. Like:
Code:
public class Player implements Runnable {
Thread t=null;
boolean isPlaying=false;
public Player(<whatevver) {
// some kinda initialization, name, shoesize...
}
public void startPlaying() {
setIsPlaying(true);
t=new Thread(this);
t.start();
}
public void stopPlaying() {
setIsPlaying(false);
}
public synchronized void setPlaying(boolean isPlaying) {
this.isPlaying=isPlaying;
}
public boolean getIsPlaying() {
return this.isPlaying;
}
public void run() {
while (getIsPlaying()) {
// do player stuff
}
}
}
Note: I've not used the Thread class' stop() method, as it is no longer considered a safe practise. The 'synchronized' directive ensures that no thread will be paused while it's accessing the synchronized methods, cause that can get messy.
12-10-2004, 09:23 PM
rogueace
thanks a lot. I'll let you know later if i need any more help.