2017-04-03 27 views
0

下面是我的MonoGame項目中的代碼,其中我在左下角有一個播放器圖像,在右上角有另一個播放器圖像。我們的目標是使用Xbox 360控制器上的左拇指把左邊的播放器1向上和向下移動。我使用了一個斷點,發現我的控制器已連接,但使用拇指杆時沒有任何反應。這裏是我的代碼:C#MonoGame:使用Xbox 360向上和向下移動Sprite圖像GamePad

using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 

namespace _2PersonShooterGame_v0._1 
{ 
    /// <summary> 
    /// This is the main type for your game. 
    /// </summary> 
    public class Game1 : Game 
    { 
     GraphicsDeviceManager graphics; 
     SpriteBatch spriteBatch; 

     // GamePad support 
     const float THUMB_STICK_DEFELCTION = 100; 

     //Window Resolution support 
     const int WINDOW_WIDTH = 1200; 
     const int WINDOW_HEIGHT = 800; 

     // player images and their rectabgles support 
     Texture2D player1; 
     Rectangle drawRect1; 
     Texture2D player2; 
     Rectangle drawRect2; 

     public Game1() 
     { 
      graphics = new GraphicsDeviceManager(this); 
      Content.RootDirectory = "Content"; 

      // Change resolution to 1200, 800 
      graphics.PreferredBackBufferWidth = WINDOW_WIDTH; 
      graphics.PreferredBackBufferHeight = WINDOW_HEIGHT; 

     } 

     /// <summary> 
     /// Allows the game to perform any initialization it needs to before starting to run. 
     /// This is where it can query for any required services and load any non-graphic 
     /// related content. Calling base.Initialize will enumerate through any components 
     /// and initialize them as well. 
     /// </summary> 
     protected override void Initialize() 
     { 
      // TODO: Add your initialization logic here 

      base.Initialize(); 
     } 

     /// <summary> 
     /// LoadContent will be called once per game and is the place to load 
     /// all of your content. 
     /// </summary> 
     protected override void LoadContent() 
     { 
      // Create a new SpriteBatch, which can be used to draw textures. 
      spriteBatch = new SpriteBatch(GraphicsDevice); 

      // Load sprites 
      player1 = Content.Load<Texture2D>(@"graphics\sprite_player1"); 
      player2 = Content.Load<Texture2D>(@"graphics\sprite_player2"); 

     } 

     /// <summary> 
     /// UnloadContent will be called once per game and is the place to unload 
     /// game-specific content. 
     /// </summary> 
     protected override void UnloadContent() 
     { 
      // TODO: Unload any non ContentManager content here 
     } 

     /// <summary> 
     /// Allows the game to run logic such as updating the world, 
     /// checking for collisions, gathering input, and playing audio. 
     /// </summary> 
     /// <param name="gameTime">Provides a snapshot of timing values.</param> 
     protected override void Update(GameTime gameTime) 
     { 
      if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) 
       Exit(); 

      // Move player1 up and down using the thumbstick on an Xbox 360 remote 
      GamePadState gamepad = GamePad.GetState(PlayerIndex.One); 

      if (gamepad.IsConnected) 
      { 
       drawRect1.Y -= (int)(gamepad.ThumbSticks.Left.Y * THUMB_STICK_DEFELCTION); 
      } 

      base.Update(gameTime); 
     } 

     /// <summary> 
     /// This is called when the game should draw itself. 
     /// </summary> 
     /// <param name="gameTime">Provides a snapshot of timing values.</param> 
     protected override void Draw(GameTime gameTime) 
     { 
      GraphicsDevice.Clear(Color.CornflowerBlue); 

      // Drawing sprites in their respective rectangles 
      spriteBatch.Begin(); 

      // Place player one in the bottom left corner and player two in the top right corner 
      drawRect1 = new Rectangle(0, WINDOW_HEIGHT - player1.Height, player1.Width, player1.Height); 
      drawRect2 = new Rectangle(WINDOW_WIDTH - player2.Width, 0, player2.Width, player2.Height); 

      spriteBatch.Draw(player1, drawRect1, Color.White); 
      spriteBatch.Draw(player2, drawRect2, Color.White); 

      spriteBatch.End(); 

      base.Draw(gameTime); 
     } 
    } 
} 

回答

1

的問題是,你設置的Update(...)方法drawRect1.Y財產,但話又說回來,你是在爲你Draw(...)方法重寫此矩形。

// update 
if (gamepad.IsConnected) 
{ 
    drawRect1.Y -= (int)(gamepad.ThumbSticks.Left.Y * THUMB_STICK_DEFELCTION); 
} 

// draw 
drawRect1 = new Rectangle(0, WINDOW_HEIGHT - player1.Height, player1.Width, player1.Height); 

你應該怎麼做我只是在你的Update(...)方法更新矩形和Draw(...)呼叫使用相同的(更新)值。

要解決此問題,請從您的Draw(...)方法中刪除此行:drawRect1 = new Rectangle(0, WINDOW_HEIGHT - player1.Height, player1.Width, player1.Height);