我正在製作多人遊戲,我在遊戲中添加玩家有一些問題。我將列表更改爲哈希集,因爲我不想公佈值。採用與(ERROR)C#更改列表哈希集,現在得到錯誤
public class Players
{
public int ID;
public string IP;
public string name;
public int locationX;
public int locationY;
public int locationXLatest;
public int locationYLatest;
public int dir;
public bool PlayerConnected = false;
public bool PlayerDisconnected = false;
}
Addind玩家在遊戲中:一是製造新的HashSet:public static HashSet<Players> PlayersList = new HashSet<Players>();
玩家級使用的foreach(WORKING遊戲
for (int i = 0; i < Globals.Globals.PlayersList.Count; i++)
{
var ent = Globals.Globals.PlayersList[i]; //<-- ERROR! WORKING IF USING LIST BUT GETTING THIS ERROR IF I'M USING HASHSET: Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.HashSet<Players>
if (ent.name != null && !ent.PlayerConnected && !ent.PlayerDisconnected)
{
ent.PlayerConnected = true;
//My secret code :D Addind player in game :3
}
}
Addind的球員,但如果LIST改進 - > CRASH)
foreach (var ent in Globals.Globals.PlayersList)
{
if (ent.name != null && !ent.PlayerConnected && !ent.PlayerDisconnected)
{
ent.PlayerConnected = true;
//My secret code :D Addind player in game :3
}
}
工作正常,如果使用列表,但新的列表項是因爲玩家添加循環檢查連接是否打開,所有的時間都是垃圾郵件。或者它可能使用列表,但沒有獲得雙重值?我嘗試做檢查是玩家添加,但然後玩家沒有顯示...
無需初始化'PlayerConnected'和'PlayerDisconnected'到'假'。他們默認已經是'假'了。 – haim770