C#MonoGame:使用Xbox 360 GamePad上下移动精灵图像

时间:2017-04-03 21:41:51

标签: c# xna monogame

以下是我的MonoGame项目中的代码,其中左下角有一个玩家图像,右上角有另一个玩家图像。目标是使用Xbox 360控制器上的左侧拇指操纵杆向上和向下移动左侧的播放器1。我使用了一个断点,发现我的控制器已经连接,但是当我使用拇指操纵杆时没有任何反应。这是我的代码:

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 个答案:

答案 0 :(得分:1)

问题在于您在drawRect1.Y方法中设置了Update(...)属性,但您又在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(...)调用中使用相同(更新)的值。

要解决此问题,只需从您的drawRect1 = new Rectangle(0, WINDOW_HEIGHT - player1.Height, player1.Width, player1.Height);方法中删除此行:Draw(...)