这是我的代码:
//Code for host
case (GameState.PlayingAsHost):
{
session.Update();
while (session.LocalGamers[0].IsDataAvailable) //Recieve data
{
NetworkGamer sender;
PacketReader reader = new PacketReader();
session.LocalGamers[0].ReceiveData(reader, out sender);
guestPaddle.Position = reader.ReadVector2();
}
hostPaddle.Update(); //Update paddle position
var packetWriter = new PacketWriter(); //Send data
packetWriter.Write(new Vector2(hostPaddle.Position.X, 50));
session.LocalGamers[0].SendData(packetWriter,SendDataOptions.InOrder);
break;
}
//Guest Code
case (GameState.PlayingAsGuest):
{
session.Update();
while (session.LocalGamers[0].IsDataAvailable)//Recieve data
{
NetworkGamer sender;
PacketReader reader = new PacketReader();
session.LocalGamers[0].ReceiveData(reader, out sender);
hostPaddle.Position = reader.ReadVector2();
}
guestPaddle.Update(); //Update paddle position
var packetWriter = new PacketWriter(); //Send data
packetWriter.Write(new Vector2(guestPaddle.Position.X, 50));
session.LocalGamers[0].SendData(packetWriter, SendDataOptions.InOrder);
break;
}
这将是一个乒乓多人游戏(因此桨的名称)。在客人在收到包裹之前发送它的包裹之前,代码工作除了作为客人玩时主机的X位置设置为客人的X位置。我正在测试是否进行更改,以便来宾和主机在发送数据之前接收数据会解决问题。通过这样做,主机会收到错误消息:“无法读取超出流的末尾”。我该如何解决这个问题?