2012-02-26 73 views
1

問題:在空格鍵按下時,我需要我的射彈發射並繼續其生命,無論另一次按下或放開空格鍵。XNA/C#射彈發射問題 - 需要一個射彈一次在空格鍵上發射新聞

下面是代碼添加,更新和繪製射彈。我已經嘗試過許多對此代碼的修改,但是我還沒有足夠的經驗來找到合適的解決方案。下面

輸入代碼在這裏下面

private void UpdateProjectiles() 
{ 
    //update projectiles 
    for (int i = projectiles.Count - 1; i >= 0; i--) 
    { 

     if (currentKeyboardState.IsKeyDown(Keys.Space) || 
      currentGamePadState.Buttons.A == ButtonState.Pressed) 
     { 

      //adds particle to first projectile but not again until the next fire butotn press 
      //particleEngine.EmitterLocation = new Vector2(projectiles[i].Position.X, projectiles[i].Position.Y); 
      projectiles[i].Update(); 
      projectileOn = true; 


     } 
     //if projectiles not being fired remove them from the game screen 
     else 
     { 
      projectiles.RemoveAt(i); 
     } 



    } 

} 

,吸引Draw方法

//add projectile if spacebar is pressed 

private void AddProjectile(Vector2 position) 
{ 
    //i need somthing like this to make bullets autofire on phone 
    //while (TouchPanel.IsGestureAvailable) 
    //{ 
    // Projectile projectile = new Projectile(); 
    // projectile.Initialize(GraphicsDevice.Viewport, projectileTexture, position); 
    // projectiles.Add(projectile); 
    //} 

    if (currentKeyboardState.IsKeyDown(Keys.Space) || 
    currentGamePadState.Buttons.A == ButtonState.Pressed) 
    { 
     Projectile projectile = new Projectile(); 
     projectile.Initialize(GraphicsDevice.Viewport, projectileTexture, position); 
     projectiles.Add(projectile); 
    } 
} 

更新彈丸代碼

AddProjectile碼彈到屏幕

//draw the projectiles 
      //*********************************************************** 
      //using the if here allows control of projectiles to pass... 
      //...to the "currentstate of the spacebar (is pressed = fire) 
      /***/ 

      if (currentKeyboardState.IsKeyDown(Keys.Space) || 
       currentGamePadState.Buttons.A == ButtonState.Pressed) 
      { 
       for (int i = 0; i < projectiles.Count; i++) 
       { 
        projectiles[i].Draw(spriteBatch); 

       } 
      } 

      //remove projectiles and reset 
      if (currentKeyboardState.IsKeyUp(Keys.Space) || 
       currentGamePadState.Buttons.A == ButtonState.Released) 
      { 


       UpdateProjectiles(); 
      } 

所以,這是我到目前爲止所說的,並且如前所述可以使彈丸良好地工作,我不能讓他們繼續他們的生活(直到碰撞或他們到達屏幕的末端)一旦我放開鍵盤空格鍵。

回答

1

A的我看是造成你的isses的問題簡要概述:

  • 如果您添加,更新過程中按下按鍵/按鈕,並繪製方法您正在檢查。
  • 如果沒有按下某個鍵,則在更新過程中刪除該項目,而在繪製過程中不繪製該項目。

要做到什麼,我認爲你要完成,我會做東西沿着這些路線(定製和清理您的需求,當然):

private void Update(GameTime gameTime) 
{ 
    // Only add a new projectile if Space or A is pressed 
    if (currentKeyboardState.IsKeyDown(Keys.Space) || 
     currentGamePadState.Buttons.A == ButtonState.Pressed) 
    { 
     AddProjectile(); // some method that adds a new projectile to 'projectiles' 
    } 

    // Update all existing projectiles, regardless of button press. 
    // This will allow your projectiles to continue flying after they have been fired. 
    for (int i = 0; i < projectiles.Count; i++) 
    { 
     projectiles[i].Update(); 
     if (projectiles[i].Dead) // check if projectiles[i] is out of bounds or has collided with something 
     { 
     projectiles.RemoveAt(i); 
     i--; 
     } 
    } 
} 

private void Draw(GameTime gameTime) 
{ 
    // Draw all existing projectiles, regardless of button press. 
    for (int i = 0; i < projectiles.Count; i++) 
    { 
     projectiles[i].Draw(spriteBatch); 
    } 
} 

你的空格鍵或一個按鈕只用於一個新的彈丸。你希望彈丸能夠飛行,直到它碰到屏幕的一邊或者飛到屏幕的邊上,它不應該取決於你是否按下了一個按鈕來做到這一點。也不應該畫子彈。

有意義嗎?

+0

嗨Jodacola,是的,謝謝,使我現在完全有意義,我會盡快回復你的建議的實施儘快現在我知道這種方法更好一點我想重寫我的代碼是abit scribbly和即時思考這些都可以重新分類成像精靈管理器和邏輯,輸入,級別經理等更好的父類,我可以問你,你認爲這種類型的模塊化是一個好辦法,因爲從長遠來看,它可能證明要更好地劃分?建議永遠受歡迎 – gruffy321 2012-02-27 14:00:12

+0

@ gruffy321在大多數情況下,分區是最好的。理想情況下,兩個對象不需要知道對方的存在。當然,這有時是不可能的,但在這種情況下,您似乎可以通過重新設計類結構來改進代碼。 – annonymously 2012-02-28 11:04:46

+0

感謝兄弟,你的意見顯然意味着很多現在已經重新編寫了所有東西,所以我可以在基礎類中處理Weponary,我的派生玩家精靈可以訪問,這個我雖然會創建一個場景,任何精靈都可以擁有子彈的能力,但是沒必要必須使用它等 – gruffy321 2012-02-28 18:06:45