DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2004

    multiplayer games

    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.


  2. #2
    Join Date
    Oct 2004
    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.

  3. #3
    Join Date
    Feb 2004
    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.

  4. #4
    Join Date
    Nov 2004
    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.

  5. #5
    Join Date
    Nov 2004
    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:

    public class Player implements Runnable {
      Thread t=null;
      boolean isPlaying=false;
      public Player(<whatevver) {
        // some kinda initialization, name, shoesize...
      public void startPlaying() {
        t=new Thread(this);
      public void stopPlaying() {
      public synchronized void setPlaying(boolean 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.
    eschew obfuscation

  6. #6
    Join Date
    Nov 2004
    thanks a lot. I'll let you know later if i need any more help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.