2012-10-29 74 views
1

我想制作一款游戏,并且我想要“敌人”或流星从右侧产卵并前往左侧。我正在研究一些代码,它似乎工作得很好,但重新生成失败。他们最终产卵一个,一个真的很慢,过了一段时间,他们根本没有产卵。所有的帮助将非常感激。XNA随机重生“敌人”

这里是我的代码:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Audio; 
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.GamerServices; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 
using Microsoft.Xna.Framework.Media; 

namespace WindowsGame2 
{ 

public class Game1 : Microsoft.Xna.Framework.Game 
{ 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 
    List<Enemies> enemies = new List<Enemies>(); 
    Random random = new Random(); 

    public Game1() 
    { 
     graphics = new GraphicsDeviceManager(this); 
     Content.RootDirectory = "Content"; 
    } 

    protected override void Initialize() 
    { 

     base.Initialize(); 
    } 
    protected override void LoadContent() 
    { 
     spriteBatch = new SpriteBatch(GraphicsDevice); 
    } 

    protected override void UnloadContent() 
    { 
    } 

    float spawn = 0; 
    protected override void Update(GameTime gameTime) 
    { 
     // Allows the game to exit 
     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
      this.Exit(); 

     spawn += (float)gameTime.ElapsedGameTime.TotalSeconds; 

     foreach (Enemies enemy in enemies) 
     { 
      enemy.Update(graphics.GraphicsDevice); 
     } 
     LoadEnemies(); 

     base.Update(gameTime); 
    } 

    public void LoadEnemies() 
    { 
     int randY = random.Next(100, 400); 
     if (spawn > 1) 
     { 
      spawn = 0; 
      if (enemies.Count() < 10) 
       enemies.Add(new Enemies(Content.Load<Texture2D>("meteor"), new Vector2(1110, randY))); 
     } 
     for (int i = 0; i < enemies.Count; i++) 
     { 
      if (!enemies[i].isVisible) 
      { 
       enemies.RemoveAt(i); 
       i--; 
      } 
     } 
    } 

    protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.CornflowerBlue); 

     spriteBatch.Begin(); 
     foreach (Enemies enemy in enemies) 
     { 
      enemy.Draw(spriteBatch); 
     } 
     spriteBatch.End(); 
     base.Draw(gameTime); 
    } 
} 
} 

,这里是我的课:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework; 

namespace WindowsGame2 
{ 
class Enemies 
{ 
    public Texture2D texture; 
    public Vector2 position; 
    public Vector2 velocity; 
    public bool isVisible = true; 

    Random random = new Random(); 
    int randX, randY; 

    public Enemies(Texture2D newTexture, Vector2 newPosition) 
    { 
     texture = newTexture; 
     position = newPosition; 

     randX = random.Next(-4, 4); 
     randY = random.Next(-4, -1); 
     velocity = new Vector2(randX, randY); 
    } 

    public void Update(GraphicsDevice graphics) 
    { 
     position += velocity; 

     if (position.Y <= 0 || position.Y >= graphics.Viewport.Height - texture.Height) 
     { 
      velocity.Y = -velocity.Y; 
     } 

     if (position.X < 0 - texture.Width) 
     { 
      isVisible = false; 
     } 
    } 
    public void Draw(SpriteBatch spriteBatch) 
    { 
     spriteBatch.Draw(texture, position, Color.White); 
    } 
} 

}

+0

这是很多代码,你可以把它裁减到相关的东西? –

+0

对,对不起。尽管我现在得到了帮助。直到下次我会记住不粘贴整个代码,对不起! – user1775668

回答

2

你的问题就出在这里。你的一些流星正在向右移动,有些并不动。

randX = random.Next(-4, 4); 

尝试使用

randX = random.Next(-4, -1); 

你的敌人/流星有时对,这是0和4之间的X的速度,所以他们向右移动。

我可以看到你改变了Y这个,也许你让他们混合起来?

+0

哦,我完全没有想到这一点。叹息,有时候解决方案可以非常简单。感谢您的帮助,非常感谢! – user1775668