1
我正在用Slick2D製作遊戲,並使用Kryonet進行多人遊戲。我的架構是爲了讓所有的客戶端連接到服務器而不是玩遊戲本身(與在同一程序中運行客戶端和服務器的玩家相反)。這是一款2D射擊遊戲,當只有一個客戶玩時,一切正常。我連接另一個客戶端時出現此問題。Kryonet:客戶端不能同時發送和接收?
顯然客戶端無法同時發送和接收。例如,如果兩個客戶同時拍攝他們的武器,看起來只有該玩家正在自己的客戶端拍攝,而另一個玩家閒置。同樣,如果兩個玩家都在移動,另一個玩家將不會移動,直到客戶端玩家停止,然後另一個玩家將跳轉到當前位置。
我提供的代碼,客戶端和服務器監聽器在這裏:
public class ClientGameListener extends Listener {
protected Client owner;
protected MultiplayerClientGameState state;
public ClientGameListener(Client c, MultiplayerClientGameState state) {
owner = c;
this.state = state;
}
@Override
public void received(Connection connection, Object obj) {
if(obj instanceof PositionPacket) {
state.adjustPlayerPosition((PositionPacket)obj);
}
else if(obj instanceof FirePacket) {
state.scheduleFireListing((FirePacket)obj);
}
else if(obj instanceof ToggleWeaponPacket) {
state.togglePlayerWeapon((ToggleWeaponPacket)obj);
}
}
}
public class ServerGameListener extends Listener {
protected Server owner;
protected MultiplayerServerGameState state;
public ServerGameListener(Server s, MultiplayerServerGameState state) {
owner = s;
this.state = state;
}
@Override
public void received(Connection connection, Object obj) {
if(obj instanceof PositionPacket) {
owner.sendToAllExceptTCP(connection.getID(), obj);
}
else if(obj instanceof FirePacket) {
owner.sendToAllExceptTCP(connection.getID(), obj);
}
else if(obj instanceof ToggleWeaponPacket) {
owner.sendToAllExceptTCP(connection.getID(), obj);
}
}
}
爲什麼我的客戶端不能同時發送和接收信息的任何想法,或如何解決這個問題?