Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
 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)
};



C#
// 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

1 solution

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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900