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.
Thanks for reading all of this and any help in advanced would be great.
randomPosition
actually returns a random position? \$\endgroup\$