2012-10-22 42 views
1

我想讓每個敵人獨立射擊一顆子彈。如果敵人的子彈離開了屏幕,敵人可以射出新的子彈。不早。但是現在,他們的屏幕總是隻有一顆而不是三顆。我有三個敵人,所以他們最多可以同時在屏幕上顯示三顆子彈,而不僅僅是一顆。 有什麼不對?屏幕上只有一顆子彈。我怎樣才能解決這個問題?

public class Map 
{ 
    Texture2D myEnemy, myBullet; 
    Player Player; 
    List<Enemy> enemieslist = new List<Enemy>(); 
    List<Bullet> bulletslist = new List<Bullet>(); 

    float fNextEnemy = 0.0f; 
    float fEnemyFreq = 3.0f; 
    int fMaxEnemy = 3; 

    Vector2 Startposition = new Vector2(200, 200); 
    Vector2 currentEnemyPosition; 

    GraphicsDeviceManager graphicsDevice; 

    public Map(GraphicsDeviceManager device) 
    { 
     graphicsDevice = device; 

    } 

    public void Load(ContentManager content) 
    { 
    myEnemy = content.Load<Texture2D>("enemy"); 
    myBullet = content.Load<Texture2D>("bullet"); 
    Player = new Player(graphicsDevice); 
    Player.Load(content); 
    } 

    public void Update(GameTime gameTime) 
    { 
     Player.Update(gameTime); 
     float delta = (float)gameTime.ElapsedGameTime.TotalSeconds; 

     for(int i = enemieslist.Count - 1; i >= 0; i--) 
     { 
     // Update Enemy 
     Enemy enemy = enemieslist[i]; 
     enemy.Update(gameTime, this.graphicsDevice, Player.playershape.Position, delta); 
     currentEnemyPosition = enemy.Bulletstartposition; 
     // Try to remove an enemy 
     if (enemy.Remove == true) 
     { 
      enemieslist.Remove(enemy); 
      enemy.Remove = false; 
     } 

     // Does the enemy shot? 
     if ((enemy.Shot == true) && (bulletslist.Count < 1)) 
     // New bullet 
      { 
      Vector2 bulletDirection = Vector2.Normalize(Player.playershape.Position - currentEnemyPosition) * 200f; 
      bulletslist.Add(new Bullet(currentEnemyPosition, bulletDirection, Player.playershape.Position)); 
      enemy.Shot = false; 
      } 
     } 

     this.fNextEnemy += delta; 
     //New enemy 
     if (fMaxEnemy > 0) 
     { 
     if ((this.fNextEnemy >= fEnemyFreq) && (enemieslist.Count < 3)) 
     { 
      Vector2 enemyDirection = Vector2.Normalize(Player.playershape.Position - Startposition) * 100f; 
      enemieslist.Add(new Enemy(Startposition, enemyDirection, Player.playershape.Position)); 
      fMaxEnemy -= 1; 
      fNextEnemy -= fEnemyFreq; 
     } 
     } 

    for(int i = bulletslist.Count - 1; i >= 0; i--) 
    { 
     // Update Bullet 
     Bullet bullets = bulletslist[i]; 
     bullets.Update(gameTime, this.graphicsDevice, delta); 

     // Try to remove a bullet... Collision, hit, or outside screen. 
     if (bullets.Remove == true) 
      bulletslist.Remove(bullets); 
     bullets.Remove = false; 
    }    
    } 

    public void Draw(SpriteBatch batch) 
    { 

     Player.Draw(batch); 
     foreach (Enemy enemies in enemieslist) 
     { 
      enemies.Draw(batch, myEnemy); 
     } 
     foreach (Bullet bullets in bulletslist) 
     { 
      bullets.Draw(batch, myBullet); 
     } 
    }  
} 

public class Enemy 
{ 
private float nextShot = 0; 
private float shotFrequency = 2.0f; 

    Vector2 vPos; 
    Vector2 vMove; 
    Vector2 vPlayer; 
    public Vector2 Bulletstartposition; 
    public bool Remove; 
    public bool Shot; 

    public Enemy(Vector2 Pos, Vector2 Move, Vector2 Player) 
    { 
     this.vPos = Pos; 
     this.vMove = Move; 
     this.vPlayer = Player; 
     this.Remove = false; 
     this.Shot = false; 
    } 

    public void Update(GameTime gameTime, GraphicsDeviceManager graphics, Vector2 PlayerPos, float delta) 
    {   
     nextShot += delta; 

     if (nextShot >= shotFrequency) 
      { 
      this.Shot = true; 
      nextShot -= shotFrequency; 
      } 

     if (!Remove) 
     { 
      this.vMove = Vector2.Normalize(PlayerPos - this.vPos) * 100f; 
      this.vPos += this.vMove * delta; 
      Bulletstartposition = this.vPos; 

      if (this.vPos.X > graphics.PreferredBackBufferWidth + 1) 
      { 
       this.Remove = true; 
      } 

      else if (this.vPos.X < -20) 
      { 
       this.Remove = true; 
      } 

      if (this.vPos.Y > graphics.PreferredBackBufferHeight + 1) 
      { 
       this.Remove = true; 
      } 

      else if (this.vPos.Y < -20) 
      { 
       this.Remove = true; 
      } 
     } 
    } 

    public void Draw(SpriteBatch spriteBatch, Texture2D myTexture) 
    { 
     if (!Remove) 
     { 
      spriteBatch.Draw(myTexture, this.vPos, Color.White); 
     } 
    } 
} 

public class Bullet 
{ 
    Vector2 vPos; 
    Vector2 vMove; 
    Vector2 vPlayer; 
    public bool Remove; 

    public Bullet(Vector2 Pos, Vector2 Move, Vector2 Player) 
    { 
     this.Remove = false; 
     this.vPos = Pos; 
     this.vMove = Move; 
     this.vPlayer = Player; 
    } 

    public void Update(GameTime gameTime, GraphicsDeviceManager graphics, float delta) 
    { 
      if (!Remove) 
      { 
       this.vPos += this.vMove * delta;     

       if (this.vPos.X > graphics.PreferredBackBufferWidth +1) 
       { 
        this.Remove = true; 
       } 

       else if (this.vPos.X < -20) 
       { 
        this.Remove = true; 
       } 

       if (this.vPos.Y > graphics.PreferredBackBufferHeight +1) 
       { 
        this.Remove = true; 
       } 

       else if (this.vPos.Y < -20) 
       { 
        this.Remove = true; 
       } 
      }   
    } 

    public void Draw(SpriteBatch spriteBatch, Texture2D myTexture) 
    { 
     if (!Remove) 
     { 
      spriteBatch.Draw(myTexture, this.vPos, Color.White); 
     } 
    } 
} 
+3

您是否嘗試過爲自己調試? –

+0

寫一個簡短的自包含和正確的例子,展示你的問題http://sscce.org –

回答

2

您需要將每個敵人與它所射擊的子彈相關聯。最簡單的方法是向Bullet類型的Enemy類和Bullet類中的Enemy類型字段添加一個字段。

(如果你想要一個更解耦方法,你可以創建一個接口IFireBullet

取而代之的檢查,如果子彈的數量少於一個,你檢查如果敵人有一個活躍的子彈。

// Does the enemy shot? 
if ((enemy.Shot == true) && (enemy.Bullet == null)) 
{ 
    … 
} 

當子彈飛出屏幕時,不要忘記清除該字段。 (這是Enemy-field有幫助的地方。)

相關問題