2016-05-16 79 views
0

各位程序員好!我很抱歉打擾你,但我有一個學校項目是創建一個遊戲。我差不多完成了,我所要做的就是讓我的角色(玩家)從敵方子彈中受到傷害。如何從敵人身上取得傷害(子彈)

這是我的敵人級別,我有我的子彈名單,這使得敵人射擊子彈。你可以看到我試圖跟蹤子彈的位置(我跟着這些教程關於使敵人https://www.youtube.com/watch?v=_TlnUM-uhSIhttps://www.youtube.com/watch?v=tfiKwOo_4xo

在你跳上我之前我只是想說我已經在各地搜索了一個答案,但因爲XNA對我想創建的內容非常有限,所以對我來說非常困難。

class Enemies 
{ 
    public Texture2D texture; 
    public Vector2 position; 
    public Vector2 velocity; 

    public bool isVisible = true; 

    Random random = new Random(); 
    int randX, randY; 
    float temp_bulletenemyX; 
    float temp_bulletenemyY; 

    // bullets 
    private List<Bullets> bullets = new List<Bullets>(); 
    Texture2D bulletTexture; 

    public Enemies(Texture2D NewTexture, Vector2 NewPosition, Texture2D newBulletTexture) 
    { 
     texture = NewTexture; 
     position = NewPosition; 

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

     velocity = new Vector2(randX, randY); 

     bulletTexture = newBulletTexture; 
    } 

    float shoot = 0; 

    public void update(GraphicsDevice graphics, GameTime gameTime) 
    { 
     KeyboardState keyboardState = Keyboard.GetState(); 
     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; 

     shoot += (float)gameTime.ElapsedGameTime.TotalSeconds; 
     if (shoot > 1) 
     { 
      shoot = 0; 
      Shootbullet(); 

     } 
     updateBullets(); 

    } 

    public void updateBullets() 
    { 
     foreach (Bullets bullet in bullets) 
     { 
      bullet.position += bullet.velocity; 
      temp_bulletenemyX = bullet.position.X; 
      temp_bulletenemyY = bullet.position.Y; 
      if (bullet.position.X < 0) 
       bullet.isVisible = false; 

    } 
     for (int i = 0; i < bullets.Count; i++) 
      if(!bullets[i].isVisible) 
      { 
       bullets.RemoveAt(i); 
       i--; 
      } 
} 
    public void Shootbullet() 
    { 
     Bullets newBullet = new Bullets(bulletTexture); 
     newBullet.velocity.X = velocity.X - 3f; 
     newBullet.position = new Vector2(position.X + newBullet.velocity.X, position.Y + (texture.Height/2) - (bulletTexture.Height/2)); 

     newBullet.isVisible = true; 
     if (bullets.Count() < 3) // hur många skott den skall skjuta 
      bullets.Add(newBullet); 


    } 
    public void draw(SpriteBatch spriteBatch) 
    { 
     foreach (Bullets bullet in bullets) 
      bullet.Draw(spriteBatch); 
     spriteBatch.Draw(texture, position, Color.White); 

    } 

    public float PosX 
    { 
     get 
     { 
      return position.X; 
     } 
    } 
    public Vector2 Pos 
    { 
     get 
     { 
      return position; 
     } 
    } 
    public float PosY 
    { 
     get 
     { 
      return position.Y; 
     } 
    } 

    public List<Bullets> GetbulletList 
    { 
     get{ 
      return bullets; 
     } 
    } 

    public Texture2D text 
    { 
     get 
     { 
      return texture; 
     } 



    } 

    public Texture2D BulletText 
    { 
     get 
     { 
      return bulletTexture; 
     } 

繼承人,我已經試過了根本的Game1代碼,以使我的性格可能受到傷害。

  player.rectangle = new Rectangle(Convert.ToInt32(player.PosX), Convert.ToInt32(player.PosY), player.text.Width, player.text.Height); 

    foreach (Enemies bullet in enemies.ToList()) 
     { 

       rec_bullet = new Rectangle(Convert.ToInt32(bullet.GetbulletList.ElementAt(i).position.X), Convert.ToInt32(bullet.GetbulletList.ElementAt(i).position.Y), nyenemy.BulletText.Width, nyenemy.BulletText.Height); 
       hit = CheckCollision(rec_bullet, player.rectangle); 
       if (hit == true) 
       { 

       player.health -= 10; 
        hit = false; 

      } 
      i++; 

如果一切都很混亂,我非常抱歉,我已經通過遵循許多教程和一些我自己的代碼將所有內容放在一起。如果我違反論壇規則,我也很抱歉,我在這裏是新的。

最好的問候基亞。

+0

您必須爲我們提供CheckCollision的代碼,並告訴我們您的代碼如何工作?子彈是否會通過你的球員或出現錯誤,或其他事情發生?你的子彈是飛行還是站立?你的球員可以移動嗎?或者是如何使它們一起工作的想法中的問題?請更準確地說明問題。 – Monset

+0

另外,您應該嘗試通過調試器運行源代碼。使用Visual Studio進行調試非常簡單,並且可以幫助您瞭解值的含義以及發生的情況。如果在windows和visual studio下調試打印將有所幫助 - Console.WriteLine()將允許您打印出變量:當一個項目符號處於活動狀態時,它就是X/Y,例如當它死亡時。 – Neil

回答

2

我會很勇敢,並提出以下建議 - 查看所提供代碼的片段。

在Game1代碼中 - 你正在通過敵人工作,但它看起來像你沒有通過每個敵人的子彈列表搜索。你用'我'獲得ElementAt(..) - 給它添加一個,然後去到下一個敵人,然後只用增加的'i'獲得ElementAt(..)。

從我的理解 - 代碼的流程應該是:

for each enemy in enemy list 
    for each bullet in enemy bullet list 
     Check Collision with Player 
      if collision then adjust player health 
0

有幾件事你的代碼錯誤。對於初學者來說,你的Bullet類應該包含bulletUpdate()方法,它應該在foreach循環中調用。

foreach (Bullet b in bullets) 
{ 
b.update(/*parameters here*/); 
} 

你的子彈名單應該是存儲在您的Game1.cs或EnemyManager類,而不是在每個敵人一個公開名單。傳遞每個敵人對該列表的引用,以便當他們觸發時,他們可以將該子彈添加到列表中。

最重要的是,您的碰撞測試應該不過是每個子彈與您自己之間的相交測試。再次,另一個foreach循環。

foreach(Bullet b in bullets) 
{ 
//where playerRect is the hitbox for your player and b.Rect is the hitbox for your bullet 
if(playerRect.Intersects(b.Rect) 
player.healh-=damage; 
} 

希望有所幫助。