0

这是我的代码:

//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位置。我正在测试是否进行更改,以便来宾和主机在发送数据之前接收数据会解决问题。通过这样做,主机会收到错误消息:“无法读取超出流的末尾”。我该如何解决这个问题?

4

1 回答 1

0

我假设你的switchandcase只是因为它可能是if's 更有效的方式。

为了重现if's 的行为,您需要将 abreak放在您的案例中或将其包含在括号中。我不确定您是否意识到这一点,或者是否是问题所在,但是您的括号似乎肯定弄乱了,因为您的客人案例在您的主机案例中。

//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();

  var packetWriter = new PacketWriter(); //Send data
  packetWriter.Write(new Vector2(hostPaddle.Position.X, 50));
  session.LocalGamers[0].SendData(packetWriter,SendDataOptions.InOrder);
}

//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();

  var packetWriter = new PacketWriter(); //Send data
  packetWriter.Write(new Vector2(guestPaddle.Position.X, 50));
  session.LocalGamers[0].SendData(packetWriter, SendDataOptions.InOrder);
}

这应该可行,我不确定方法中有什么,所以你可能不得不*paddle.Update()离开这些案例。

于 2013-10-23T23:30:35.733 回答