将对象添加到列表时出现问题 [英] Problem with objects added to a list

查看:64
本文介绍了将对象添加到列表时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个小型游戏,并且拥有所有各种各样的敌人的数组(敌人是Character对象,因此它是这些对象的数组),以及在战斗中可容纳玩家和所有为当前战斗选择的敌人.但是,每当一个敌人中的两个被添加到列表中时,它就会同时填补两个列表位置,但似乎将其视为同一事物,它们的更新位置以及列表中每个相同类型的敌人的一切都会发生变化.

I''m working on a small game and have an array of all the various enemies (enemies are a Character object so its an array of these objects), as well as a list that, during battle, holds the player and all the enemies that have been selected for the current battle. But whenever two of an enemy are added to the list, it fills both list slots but seems to treat then as the same thing, their updated positions and everything get changed for every enemy of the same type in the list.

 public static Objects.Character[] EnemyList =
{
    new Objects.Character("Demon", 1, 70, 0, 70, 0, 1, 20, 0, 20,
        new Microsoft.Xna.Framework.Rectangle(0, 0, 0,0), 0, 0, 2, 0),
    new Objects.Character("Ant", 2, 15, 0, 20, 0, 10, 5, 0, 5,
        new Microsoft.Xna.Framework.Rectangle(0, 0, 0,0), 0, 0, 3, 0),
    new Objects.Character("Giant", 3, 25, 30, 25, 30, 3, 15, 10, 15,
        new Microsoft.Xna.Framework.Rectangle(0, 0, 0,0), 0, 0, 3, 0),
    new Objects.Character("Ghost", 4, 30, 20, 30, 20, 3, 20, 10, 20,
        new Microsoft.Xna.Framework.Rectangle(0, 0, 0,0), 0, 0, 1, 0),
    new Objects.Character("Dragon", 5, 20, 40, 20, 40, 2, 20, 0, 20,
        new Microsoft.Xna.Framework.Rectangle(0, 0, 0,0), 0, 0, 1, 0)
};







// Add all characters to the list of turns
Random random = new Random();
Objects.Character turn;
int numb = 1;
TurnOrder.Add(Game1.PlayerCharacter);
for (int i = 0; i < 2; i++)//change back to 5
{
    turn = GameContent.Enemies.EnemyList[random.Next(0,                                                     GameContent.Enemies.EnemyList.Length - 1)];
    while (turn.GameLevel > 6)
    { turn = GameContent.Enemies.EnemyList[random.Next(0, GameContent.Enemies.EnemyList.Length - 1)]; }
    TurnOrder.Add(turn);
}

// Organize all characters in battle according to their speed
for (int i = 0; i < TurnOrder.Count; i++)
{
    for (int y = 0; y < i; y++)
    {
        if (TurnOrder[i].Speed > TurnOrder[y].Speed)
        {
            turn = TurnOrder[i];
            while (i - numb >= y)
            {
                TurnOrder[i - (numb - 1)] = TurnOrder[i - numb];
                numb++;
            }
        }
    }
}
TurnOrder[CurrentTurn].MovesLeft = TurnOrder[CurrentTurn].Speed;



我记得以前曾读过一些有关列表问题的信息,所以我很确定我添加代码的代码是正确的,无论是哪种方式我都再次查看过,什么也没看到.反正有解决这个问题的方法吗?还是我错过了我做错的事情?



I remember reading something about this kind of problem with lists before, so I''m pretty sure my code for adding things in is fine, either way I''ve looked over it again and am not seeing anything. Is there anyway to get around this problem? Or am I missing something I did wrong?

推荐答案

您使用的实际上不是列表,而是一个Objects.Character的数组.难怪它不支持列表接口.不要使用数组,而要使用列表!您将需要类型System.Collections.Generic< Objects.Character>的列表.请参阅 http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx [ ^ ].请注意:您始终可以通过调用方法ToArray来获取数组.

如果需要通过某个成员的值和非常快速的搜索(大型集合的O(1)复杂度)来保持对象的唯一性,请考虑使用System.Collections.Generic.DictionarySystem.Collections.SortedDictionarySystem.Collections.Generic.SortedList.它们具有不同的内存开销和不同的速度,因此请查看每个MSDN帮助页面以选择最适合您的页面和/或进行一些试验.请参阅 http://msdn.microsoft.com/en-us/library/0sbxh9x2.aspx [^ ].

(Objects是类的名称空间吗?不是很好的名称!请避免使用诸如Object,Application,Class等的著名名称.如果这是一个类,则其名称不应为复数.)

—SA
What you use is not really a list, but is an array of Objects.Character. No wonder it does not support list interfaces. Instead of array, do use the list! You will need the list of the type System.Collections.Generic<Objects.Character>. See http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx[^]. Pay attention: you can always get an array by calling the method ToArray.

If you need to maintain uniqueness of object by the value of some member and very fast search (O(1) complexity for big collections), consider using System.Collections.Generic.Dictionary, System.Collections.SortedDictionary or System.Collections.Generic.SortedList. They have different memory overheads and different speeds, so look at each MSDN help page to select the one best for you and/or do some experimenting. See http://msdn.microsoft.com/en-us/library/0sbxh9x2.aspx[^].

(Is Objects a namespace of class? Not very good name! Avoid well-known names like Object, Application, Class, etc. If this is a class, its name should not be plural.)

—SA


这篇关于将对象添加到列表时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆