我有小行星在我的游戏中产生,它们产生时有一个计时器,我希望它们一次产生一个。我的问题是它们都在同一行产卵,所以当它们提高速度时,它们会在已经几乎离开屏幕的小行星上产卵。这就是它的样子。
http://www.youtube.com/watch?v=9pKXZyzIpGs&feature=youtu.be
这是我的更新代码。
namespace PixeBlastGame
{
public class GameUpdate
{
SpriteBatch spriteBatch;
GameTime gameTime;
Vector2 vPlayer = new Vector2(0, 720 / 2);
Vector2 vAsteroid;
Vector2 velocity;
Random rand = new Random();
float spawnTimer;
float spawnRate = 1;
List<Vector2> Asteroids = new List<Vector2>();
public GameUpdate()
{
vAsteroid.Y = rand.Next(100, 680);
vAsteroid.X = 1100;
}
public void Update(GameTime theTime)
{
gameTime = theTime;
vPlayer.Y += velocity.Y;
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
spawnTimer += elapsed;
if (spawnTimer >= spawnRate)
{
Asteroids.Add(new Vector2(1000 ,rand.Next(0, 720)));
spawnTimer = 0;
}
if (Keyboard.GetState().IsKeyDown(Keys.Down))
{
velocity.Y = 5;
}
else if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
velocity.Y = -5;
}
else
{
velocity.Y = 0;
}
if (vPlayer.Y > 630 )//+ Game1.tPlayer.Height)
vPlayer.Y = 630;
if (vPlayer.Y < -10) //+ Game1.tPlayer.Height)
vPlayer.Y = -10;
}
public void Draw(SpriteBatch theBatch)
{
spriteBatch = theBatch;
foreach (Vector2 asteroid in Asteroids) //Draw each astroid
{
DrawAsteroid(asteroid.X, asteroid.Y);
}
spriteBatch.Begin();
spriteBatch.Draw(Game1.tPlayer, vPlayer, Color.White);
spriteBatch.End();
}
public void DrawAsteroid(float x, float y)
{
vAsteroid.X -= 5; vAsteroid.Y = y;
spriteBatch.Begin();
spriteBatch.Draw(Game1.tAsteroid, vAsteroid, Color.White);
spriteBatch.End();
}
}
}
如果您需要提供更多代码来帮助我,请发表评论。