Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# list
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?
Posted 4-Aug-11 20:05pm
Sonar87421

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Mathew Soji 270
1 BillWoodruff 260
2 Sergey Alexandrovich Kryukov 240
3 OriginalGriff 236
4 Afzaal Ahmad Zeeshan 208
0 OriginalGriff 6,168
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,028
3 Manas Bhardwaj 4,539
4 Maciej Los 3,845


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 5 Aug 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100