I was working on a game where I have a bunch of enemies. I wanted the game to randomly select which enemy to use but it always picks the same one. Here is the file were I make the enemies:

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace AlienAttack
{
	public class Enemy : Sprite
	{

        private int RandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }

        // which direction are we moving through the animation set?
		private int direction = 1;

		double lastTime;

		public Enemy(ContentManager contentManager)
		{
			spriteTextures = new Texture2D[10];

            // load the spriteTextures
            if(RandomNumber(1,2) == 1)    
                for (int i = 0; i <= 9; ++i)
                    spriteTextures[i] = contentManager.Load<Texture2D>("gfx\\enemy1\\enemy1_" + i);
            if(RandomNumber(1,2) == 2)
                for (int i = 1; i <= 9; ++i)
                    spriteTextures[i] = contentManager.Load<Texture2D>("gfx\\enemy2\\enemy2_" + i);
            
			this.Velocity.X = 1;
		}

		public override void Update(GameTime gameTime)
		{
			// if we're at the end of the animation, reverse direction
			if(frameIndex == 9)
				direction = -1;

			// if we're at the start of the animation, reverse direction
			if(frameIndex == 0)
				direction = 1;

			// every 70ms, move to the next frame
			if(gameTime.TotalGameTime.TotalMilliseconds - lastTime > 70)
			{
				frameIndex += direction;
				lastTime = gameTime.TotalGameTime.TotalMilliseconds;
			}
		}
	}
}

And then here is the code that loads these enemies into the game:

enemies = new Enemy[EnemyRows,EnemyCols];

			// create a grid of enemies
			for(int y = 0; y < EnemyRows; y++)
			{
				for(int x = 0; x < EnemyCols/2; x=x+2)
				{
					Enemy enemy = new Enemy(contentManager);
					enemy.Position.X = x * enemy.Width + EnemySpacing.X;
					enemy.Position.Y = y * enemy.Height + EnemySpacing.Y;
					enemies[y,x] = enemy;
				}
			}

Any ideas on what is wrong?

Recommended Answers

All 7 Replies

I still don't see where I went wrong. It still always chooses the first one.

Declare random variable at class-level and then change the RandomNumber method like this this:

public class Enemy : Sprite
	{

        // Declared at class-level
    
        Random random = new Random();
 
        private int RandomNumber(int min, int max)
        {
            return random.Next(min, max);
        }
.......

Declare random variable at class-level and then change the RandomNumber method like this this:

public class Enemy : Sprite
	{

        // Declared at class-level
    
        Random random = new Random();
 
        private int RandomNumber(int min, int max)
        {
            return random.Next(min, max);
        }
.......

It still always chooses the first enemy.

Got the problem now.

You have used RandomNumber(1,2) which means minimum 1 and less than 2 which will always show 1. If you want to randomly get 1 or 2 then use:

RandomNumber(1,3)

Thanks

Thanks...I had tried that earlier but the game kept crashing when I did but I figured that out now. Thanks a bunch.

If you would have read the site I have giving you, you would have noticed that calling new Random() in your setting, produces the same random sequence every time. Suggest you read this site from the first to the last letter and even execute and follow the examples given. If that does not provide for some enlightenment, I can give you a tip: use a seed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.