2012-06-02 19 views
1

我正在爲我的朋友們開發一款小型遊戲。到目前爲止,我已經獲得了網絡的權利,兩個玩家都可以四處飛翔,並且全部同步。從錯誤玩家射擊的投射物

現在我已經加入彈(激光),我產卵這樣的:

if (_mou.LeftButton == ButtonState.Pressed 
     && oldState.LeftButton 
     != ButtonState.Released)    
     { 
      if (timeSinceShot > timePerShot) 
      { 
       timeSinceShot = 0; 
       bulletRotation = rotation; //Rotation of the players ship 
       laser.addLaser(myID, bulletRotation, localPosition); 
      } 
     } 

這工作得很好,它觸發從我的船激光器,但沒有得到顯示呢。

現在,當我火我稱之爲:

om.Write(bulletRotation); //Sends the rotation to the server 

而且當服務器收到它,它發回給所有玩家,包括誰開槍的人。

以下是我收到的客戶端上的數據,並將其寫入激光列表:

if (who != myID) 
{ 
    try 
    { 
     float laserR = msg.ReadFloat(); 
     laser.addLaser(who, laserR, player.players[i].position); 
    } 
    catch { } 
} 

現在,當我測試它在2個客戶端和火,我可以看到自己射擊在第二屆客戶端,它是好。然而,它不僅會觸發第二位客戶,還會觸發我客戶的第二位玩家。

編輯:誰是RemoteUniqueIdentifier和身份識別碼是客戶RemoteUniqueIdentifier

這裏是我的問題的照片。 http://i.stack.imgur.com/CYJyW.png (不能上傳尚未因爲我沒有10代表。)

編輯2:

這是怎樣的服務器時,它的數據發送給所有的玩家:

foreach (NetConnection player in server.Connections) 
        { 
         // ... send information about every other player (actually including self) 
         foreach (NetConnection otherPlayer in server.Connections) 
         { 
          // send position update about 'otherPlayer' to 'player' 
          NetOutgoingMessage om = server.CreateMessage(); 

          // write who this position is for 
          om.Write(player.RemoteUniqueIdentifier); 
          om.Write(otherPlayer.RemoteUniqueIdentifier); 

          if (otherPlayer.Tag == null) 
           otherPlayer.Tag = new float[4]; 

          float[] pos = otherPlayer.Tag as float[]; 

          om.Write(pos[0]); // velocity X 
          om.Write(pos[1]); // velocity X 
          om.Write(pos[2]); // rotation 

          if (!noLasers) 
          { 
           om.Write(pos[3]); // bullet rotation 
          } 

          // send message 
          server.SendMessage(om, player, NetDeliveryMethod.Unreliable); 
         } 
        } 
+0

玩家中的'i' [i]'??? –

+0

for(int i = 0; i

+0

OK,並且當您調試並設置斷點以放置IF後面時,它會執行多少次? –

回答

0

如果RemoteUniqueIdentifier是某個類的對象,並且您試圖通過電話天真地傳遞它,您將永遠無法測試本地版本以及從服務器獲得的相等版本。

'另一面'上的重建對象將始終具有無法與原始對象進行比較的引用。

在這種情況下,你可能有一個選項,從RemoteUniqueIdentifier切換到一些不可改變的類型,如intGuid,甚至string會工作。

+0

RemoteUniqueIdentifier在服務器上和在客戶端上是一樣的。 –

+0

請告訴我它是什麼?這是一種什麼類? –

+0

我正在使用Lidgren網絡庫。RemoteUniqueIdentifier是一個隨機生成的密鑰,只要你連接到服務器,當你重新連接它時,它就會被使用。 –