multiplayer games


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: multiplayer games

  1. #1
    Join Date
    Nov 2004
    Posts
    13

    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.

    Thanks,
    rogueace

  2. #2
    Join Date
    Oct 2004
    Posts
    311
    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
    Posts
    541
    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
    Posts
    13
    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
    Location
    Norway
    Posts
    1,560
    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.
    eschew obfuscation

  6. #6
    Join Date
    Nov 2004
    Posts
    13
    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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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