2015-08-23 74 views
0

我正在製作2D空間射擊遊戲,我遇到了一個屏蔽電源問題。盾牌使得玩家在敵人船隻碰撞一段時間後無敵。當玩家碰撞加電時,玩家離開加電位置一段時間後立即無敵,加電從屏幕上移除,一切正常。然而,我所遇到的問題是玩家仍然可以與其進行碰撞,即使它被從畫面中移出,這意味着玩家可以簡單地呆在那個地方,永遠無敵。所以一旦玩家與盾牌發生碰撞,我就會喜歡它,所以玩家不能再次與它碰撞。我嘗試過在線搜索,但沒有發現任何特別有用的東西。 我也想再次使用這個功能。在未來,我計劃在我的滾動背景中「隨機」添加它們。不知道這是否會添加任何內容只是想我會包括它。非繪製雪碧仍然活躍xna

game1.cs

public double counterPower = 0; 

    public bool powerUpCollision = false; 
    public bool invincibility = false; 
    Sprite shieldPower; 
    bool isVisible = true; 

    protected override void Initialize() 
    { 
     shieldPower = new Sprite(); 
    } 

    protected override void LoadContent() 
    { 
     if (isVisible == true) 
     { 
      shieldPower.LoadContent(this.Content, "powerUpShield"); 
      shieldPower.Position.X = 300; 
      shieldPower.Position.Y = 300; 
     } 
    } 

    protected override void Update(GameTime theGameTime) 
    { 
     powerUps(theGameTime); 
    } 

    public void powerUps (GameTime theGameTime) 
    { 
     if (mPlayer.boundingBox.Intersects(shieldPower.boundingBoxShieldPower)) 
     { 
      if (shieldPower.isVisible == true) 
      { 
       Console.WriteLine("collision working"); 
       powerUpCollision = true; 
       invincibility = true; 
       isVisible = false; 
       if (powerUpCollision == true && invincibility == true) 
       { 
        lives = lives - 0; 
       } 

       counterPower = 0; 
      } 
     } 
     counterPower += theGameTime.ElapsedGameTime.TotalSeconds; 
     { 
      Console.WriteLine(counterPower); 
      if (counterPower > 7) 
      { 
       powerUpCollision = false; 
       invincibility = false; 

      } 
     } 


     protected override void Draw(GameTime theGameTime) 
     { 
      spriteBatch.Begin(); 

      if (isVisible == true) 
      { 
       shieldPower.Draw(this.spriteBatch); 
      } 
     } 
     spriteBatch.End(); 

在精靈類是開機的邊框,這工作正常。 我有更多的代碼和類,但我相當肯定它有很多與這個問題無關,我也不想浪費你的時間。

任何幫助將不勝感激。 謝謝。

+0

找不出如何修改的信息(我是新來的#1),但我也想再次使用這個權力了。在未來,我計劃在我的滾動背景中「隨機」添加它們。不知道這是否會添加任何內容只是想我會包括它。 – Anonymous5642

+0

您可以使用標籤下方的「編輯」鏈接(評論部分上方)編輯帖子。 –

+0

謝謝,Ben N. – Anonymous5642

回答

0

我會首先建議的是將邊界框設置爲null,或者將其設置爲無法與其碰撞的地方。這聽起來像是玩家船與物體相撞,因爲即使它沒有被顯示,它的所有屬性仍然存在於系統中。因此,將其隱藏並隱藏在屏幕外是一種選擇,但如果在應該調用它時使用其他代碼設置位置,那也是一種選擇。

第二件事,如果遊戲中存在powerup,則設置一個標誌。基本上在if語句中嵌入所有「在船舶撞擊時執行此操作」,以便只在需要時適用,並在條件不滿足時跳過該代碼。如果邊界框不會影響其他任何東西,你可以做到這一點,而不是第一部分。

編輯:爲了澄清,這樣的事情:

if(ship.hitsPowerup) 
{ 
    if(powerup.isActive) 
    { 
     doTheThing; 
     powerup.isActive = false; 
    } 
} 
+0

嗨馬特,我已經想通了。非常感謝你的幫助。我一整天都被困住了。 – Anonymous5642

+0

沒有問題,我有很多經驗,「我的代碼不工作......我不知道爲什麼」「我的代碼工作....我不知道爲什麼」感覺hahaha –

+0

雖然它的思考實際上,當你考慮這個問題時並不是那麼複雜的修復,只是自己看不到修復。我花了整整一天的時間。你一直很有幫助。 @Matt M – Anonymous5642