Jump to content

Problem with objects added to a list

- - - - -

  • Please log in to reply
1 reply to this topic

#1
sonar87

sonar87

    Newbie

  • Members
  • PipPip
  • 15 posts
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?

#2
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 243 posts
Firstly, you shouldn't subtract one from the value in this line and the other line like it. random.Next generates numbers starting from the first value up to one less than the ending value. You'll never get your last enemy with your code.
GameContent.Enemies.EnemyList[random.Next(0, GameContent.Enemies.EnemyList.Length - 1)];

What is GameLevel? You don't show the definition of the enemy object so I can't tell which of the values is supposed to be GameLevel and which actually is GameLevel (which could be your problem).

Using a do/while loop would reduce your code (by one whole line :)) You'll only need to generate enemies one time instead of the two you are doing now
do {

    turn = GameContent.Enemies.EnemyList[random.Next(0, GameContent.Enemies.EnemyList.Length - 1)];

} while (turn.GameLevel > 6);

It looks like you are trying to sort everything by speed. Your sort is less than optimal, and I suspect it is the cause of your problems. I don't have the time right now to prove it wrong (:)) but I'd rethink this portion. Using List.Sort with your own compare method would be better.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users