0
\$\begingroup\$

I am making a game for college in XNA C#, however I have got to the point where I need some enemy sprites to be spawned in for the player to shoot. I did this with one enemy by itself without using arrays and for loops to load and update all the enemy's at once as I have my enemy sprites inside a class. The class I have it supposed to give the enemy a random position, load the texture in and then draw them.

So I thought I would put my enemy sprites in a form of a array, and use a for loop to load, update and draw them. However, it all works but they all spawn in the random position on top of each other, but this works with one enemy at a time without for loops or putting them in an array.

Here is some of the code I used:

This is how I decide how many enemy sprite I want.

giantManager[] giant = new giantManager[7];

Because I have to create an instants of the giantManager class before the load content I use this to do so:

        for (int i = 0; i < 7; i++)
        {
            giant[i] = new giantManager();
        }

The following is how I load the giantManager class with the texture for my giants in the load function:

            for (int i = 0; i < 7; i++)
            {
                giant[i] = new giantManager(Content.Load<Texture2D>("giant"));
            }

Then to draw them I just use:

            for (int i = 0; i < giant.Length - 1; i++)
            {
                giant[i].Draw(spriteBatch);
            }

And this is the draw method inside the class:

public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(giantTexture, new Rectangle((int)position.X, (int)position.Y, giantTexture.Width, giantTexture.Height), null, Color.White, giantAngle, new Vector2(giantTexture.Width / 2, giantTexture.Height / 2), SpriteEffects.None, 0);
    }

To choose my random spawn location I just use the Random C# data type and use if statements to decide which spawn location to set depending on the Random number generated. This happens in the constructor here:

    public giantManager(Texture2D newTexture)
    {
        giantTexture = newTexture;
        position = randomPosition();
    }

Just to conclude this question I would like to say sorry if I have posted to much code or not explained it in enough detail, I have tried by best.

Also if you look at this image below, it shows the rectangles in a black box, now the player rectangle is a little see through, but the giant rectangle is totally black which is because they all spawn on top of each other. That is my problem.

enter image description here Thanks for reading all of this and any help in advanced would be great.

\$\endgroup\$
2
  • \$\begingroup\$ Did you make sure that randomPosition actually returns a random position? \$\endgroup\$
    – bummzack
    May 31, 2015 at 12:33
  • \$\begingroup\$ Yes as It works with one giant. Also I have just double checked it and yes it does. Just to add, I find it very weird that this works if I use one giant but when I use an array and for loops to use many giants, in my case 7 it is like they are all linked up together :/ \$\endgroup\$
    – Guest182
    May 31, 2015 at 12:36

1 Answer 1

1
\$\begingroup\$

It is probably because you are creating a new Random object in randomPosition().

You should create one Random instance and then use Next() to generate further unique random numbers. Otherwise the Random objects are instanced with the same seed (which is the current time) and thus return the same random number.

You can solve this by passing a Random object to randomPosition() and using it to generate the random numbers.

For example:

public Point randomPosition(Random r)
{
    return new Point(r.Next(), r.Next());
}

Then the create Giant function also needs to take a Random object as an argument which passes that argument to the randomPosition function.

Giant createGiant(Texture t, Random r)
{
    ...
    Pos = randomPosition(r);
}

And in your for loop:

Random r = new Random();
for(...)
{
    giant = CreateGiant(texture, r);
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .