2017-04-03 10 views
0

다음은 MonoGame 프로젝트의 코드입니다. 왼쪽 하단에 플레이어 이미지가 있고 오른쪽 상단에 다른 플레이어 이미지가 있습니다. 목표는 Xbox 360 컨트롤러에서 왼쪽 썸 스틱을 사용하여 왼쪽에서 플레이어 1을 위아래로 이동하는 것입니다. 내가 브레이크 포인트를 사용하고 내 컨트롤러가 연결되어 있지만 썸 스틱을 사용할 때 아무런 반응이 없다는 것을 알았습니다.C# MonoGame : Xbox 360을 사용하여 스프라이트 이미지 위아래로 이동 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);을 삭제하면됩니다.