1
votes

So in creating a tutorial using xna I learned to spawn sprites from the right side moving left using this code

Vector2 position = new Vector2(GraphicsDevice.Viewport.Width +enemyTexture.Width / 2, random.Next(100, GraphicsDevice.Viewport.Height -100));

I wanted to change it so sprites would appear at the bottom and move to the top so I tried change it to this

Vector2 position = new Vector2(GraphicsDevice.Viewport.Height + enemyTexture.Height / 2, random2.Next(100, GraphicsDevice.Viewport.Width - 100));

It kind of works but sometimes this results in the enemy spawning in the middle of the screen instead of the bottom. What did I do wrong?

1

1 Answers

0
votes

The Vector2 structure asks for two parameters corresponding to the X and Y axis respectively.

You are setting your position in the wrong way by setting the X value to whatever is the bottom of the screen and the Y to "somewhere" on the screen by using random.

Try changing the order of the parameters like this:

Vector2 position = new Vector2(random2.Next(100, GraphicsDevice.Viewport.Width - 100), GraphicsDevice.Viewport.Height + enemyTexture.Height / 2);

That way you are setting:

X = somewhere randomly along the X axis of the screen. Y = The bottom of the screen - half the height of your sprint (are you sure you want to use half?).