for school my friend and I are making a 2D platform game in java. The game itself works fine but we are having some trouble with the multiplayer part of it.
Our game is kind of simple, just a little guy and some enemies.
I never programmed some connection between 2 PC's in java so I based my "connection" code on some tutorials on youtube about a chat app. The connection works fine but the problem was that the program only read input from socket, but doesn't run the game anymore.
I managed to fix that just by getting it out of a while(true)
loop but now I have another problem that I can't solve right now.
When starting a multiplayer game you can choose whether you want to create a game (Server) or if you want to join a game (Client).
When you create a game, it will wait for a connection (Client) to connect before starting. But now when the whole game should start it's "stuck". It's not really stuck actually. Suppose you're the server, and your friend connects on your server as a Client. He joins your game and on both computers the game starts but you stay in the menu. Only when your friend (Client) hits a button the game at your screen (Server) will move (each time your friend hit a button the game will move a bit) and if you hit a button, your friends game will move. If both server and client keep their finger on a button (left, right, jump, fire...) the game works perfectly but of course this is not good.
I think the problem is in this line of code:
if(ServerState.server == true){
try {
Server.clientMessage = (String) Server.serverInput.readObject();
System.out.println(Server.clientMessage);
}
catch (ClassNotFoundException e) {
System.out.println("Data type not found");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
if(ClientState.client == true){
try {
Client.serverMessage = (String) Client.clientInput.readObject();
System.out.println(Client.serverMessage);
}
catch(ClassNotFoundException e){
System.out.println("Data type not found");
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
serverInput
and clientInput
are both ObjectInputStreams
.
The variable serverState.server
and ClientState.client
are just booleans
to see if you're server or client.
This code is implemented in the code for the "online level".
If I just comment this code the game works but the "connectedPlayer" doesn't move (Server/Client doesn't get any information from Client/Server)
My question is: Does this line of code :
Server.clientMessage = (String) Server.serverInput.readObject();
waits for an input, and when he gets that the program can continue? And if there is no input, the whole program stops? Any ideas for solving this problem if my explanation is clear enough.
Thank you in advance!
PS: sorry for my bad English :s
readObject
blocks until input is available, then returns the input when it arrives. You will need to create a new thread to read input and give the input to the other parts of the game.
To pass information to the game, you will probably want to create different methods in the Server
and Client
classes to handle different messages. You can use a switch
or if else if
to decide which method to use based on either a field in the object or the type of object (using instanceof
).