2013-04-04 200 views
1

好的我有一個包含移動代碼等的球員類 我的問題是當按鍵「2」被按下時我希望玩家精靈(默認是徒手)切換到一個人槍。切換小精靈時出現問題

我得到了這個工作,直到我試圖在遊戲中添加第三支槍,此時我只能通過按住「2」和「3」鍵看到兩支槍。

我希望它切換,我覺得我的使用布爾可能是原因,但這裏的代碼現在

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; 
using Microsoft.Xna.Framework.Net; 
using Microsoft.Xna.Framework.Storage; 

namespace Teir_Tactical_2A 
{ 
    public class Player 
    { 
     public Texture2D playerunarmed; 
     public Texture2D playerM1911; 
     public Texture2D playerM4; 
     public KeyboardState keyState; 
     public KeyboardState oldKeyboardState; 
     public Vector2 playerPosition; 
     public SpriteBatch spriteBatch; 
     public float Angle { get; set; } 
     public float AngularVelocity { get; set; } 
     public Vector2 playerVelocity = Vector2.One; 
     public bool unarmed; 
     public bool M1911; 
     public bool M4; 
     public bool IsToggled = false; 
     float angle; 

     public Player(ContentManager content, Vector2 location) 
     { 
      this.playerunarmed = content.Load<Texture2D>("PLAYER"); 
      this.playerM1911 = content.Load<Texture2D>("PLAYERM1911"); 
      this.playerM4 = content.Load<Texture2D>("PLAYERM4"); 
      playerPosition = location; 
      unarmed = true; 
      M4 = true; 
      M1911 = true;   
     } 

     public void Update() 
     { 
      MouseState curMouse = Mouse.GetState(); 

      if (M1911 == true) 
      { 
       armed(); 
      } 

      if (unarmed == true) 
      { 
       armed(); 
      } 

      if (M4 == true) 
      { 
       armed(); 
      } 

      Vector2 mouseLoc = new Vector2(curMouse.X, curMouse.Y); 
      Vector2 direction = mouseLoc - playerPosition; 
      angle = (float)(Math.Atan2(direction.Y, direction.X)); 
      keyState = Keyboard.GetState();   

      if (keyState.IsKeyDown(Keys.D1)) 
      { 
       unarmed = true; 
       M4 = false; 
       M1911 = false;    
      } 

      if (keyState.IsKeyDown(Keys.D2)) 
      { 
       M1911 = true; 
       M4 = false; 
       unarmed = false;   
      } 

      if (keyState.IsKeyDown(Keys.D3)) 
      { 
       M4 = true; 
       unarmed = false; 
       M1911 = false; 
      }      

      if (keyState.IsKeyDown(Keys.D)) 
       playerPosition.X += playerVelocity.X + 1; 
      if (keyState.IsKeyDown(Keys.A)) 
       playerPosition.X -= playerVelocity.X + 1; 
      if (keyState.IsKeyDown(Keys.W)) 
       playerPosition.Y -= playerVelocity.Y + 1; 
      if (keyState.IsKeyDown(Keys.S)) 
       playerPosition.Y += playerVelocity.Y + 1; 
     } 

     public void Draw(SpriteBatch spriteBatch) 
     { 
      spriteBatch.Begin(); 
      { 
       Rectangle sourceRectangle = new Rectangle(0, 0, playerunarmed.Width, playerunarmed.Height); 
       Vector2 origin = new Vector2(playerunarmed.Width/2, playerunarmed.Height/2); 

       if (unarmed == true) 
       { 
        spriteBatch.Draw(playerunarmed, new Rectangle((int)playerPosition.X, (int)playerPosition.Y, playerunarmed.Width, playerunarmed.Height), null, Color.White, angle + 90, new Vector2(62, 70), SpriteEffects.None, 0f); 
       } 

       if (M1911 == true) 
       { 
        spriteBatch.Draw(playerM1911, new Rectangle((int)playerPosition.X, (int)playerPosition.Y, playerunarmed.Width, playerunarmed.Height), null, Color.White, angle + 90, new Vector2(62, 70), SpriteEffects.None, 0f); 
       } 

       if (M4 == true) 
       { 
        spriteBatch.Draw(playerM4, new Rectangle((int)playerPosition.X, (int)playerPosition.Y, playerunarmed.Width, playerunarmed.Height), null, Color.White, angle + 90, new Vector2(62, 70), SpriteEffects.None, 0f); 
       } 
      } 
      spriteBatch.End(); 
     } 

     public void armed() 
     { 
      M1911 = false; 
      M4 = false; 
      unarmed = true; 
     } 
    } 
} 

繼承人,我認爲這個問題是,它在這裏

if (keyState.IsKeyDown(Keys.D1)) 
{ 
    unarmed = true; 
    M4 = false; 
    M1911 = false; 

} 

if (keyState.IsKeyDown(Keys.D2)) 
{ 
    M1911 = true; 
    M4 = false; 
    unarmed = false;   
} 

if (keyState.IsKeyDown(Keys.D3)) 
{ 
    M4 = true; 
    unarmed = false; 
    M1911 = false; 
} 

不知何故,我必須讓鍵盤切換,這樣當我按下「3」時,它確保M1911和非武裝變量設置爲false,M4值設置爲true。

我對此有何看法?

我也試過IsKeyUp & & IsKeyDown方法,在這裏你當前的狀態鏈接到老態,但只造成槍的閃爍在屏幕上breifly它自敗,所以如果那裏有一個辦法解決這個問題之前, ,那麼這將是偉大的

編輯:那麼有沒有辦法編輯武裝的方法,以便當一個鍵被按下武器狀態被改變?

2ND編輯:我想通了,我給每個武器狀態自己的武裝();變量,然後在底部的武裝()中添加true/false設置;可變這裏:

public void armedM1911() 
    { 
     M1911 = true; 
     M4 = false; 
     player = false; 

    } 
    public void armedM4() 
    { 
     M4 = true; 
     M1911 = false; 
     player = false; 
    } 
    public void unarmed() 
    { 
     M1911 = false; 
     M4 = false; 
    } 
} 

}

回答

0

你調用在每個更新循環的開始armed()方法不管你的武器狀態

if (M1911 == true) 
{ 
    armed(); 
} 

if (unarmed == true) 
{ 
    armed(); 
} 
if (M4 == true) 
{ 
    armed(); 
} 

這顯然要求

public void armed() 
{ 
    M1911 = false; 
    M4 = false; 
    unarmed = true; 
} 

它將你的武器設置爲假,unarmed true

因此,即使您認爲您可能會在循環結束時切換您的狀態,但當您釋放鍵時,它總會在下一個循環中再次復位。