简体   繁体   English

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

[英]C# MonoGame: Moving Sprite Image Up and Down Using Xbox 360 GamePad

Here is the code from my MonoGame project where I have one player image in the bottom left corner and another player image in the top right. 这是我的MonoGame项目中的代码,在其中我在左下角有一个玩家图像,在右上角有另一个玩家图像。 The goal is to use the left thumbstick on an Xbox 360 controller to move the player 1 on the left up and down. 目的是使用Xbox 360控制器上的左指杆来上下移动播放器1。 I used a break point and found that my controller is connected, but nothing happens when I use the thumbstick. 我使用了一个断点,发现控制器已连接,但使用指拨杆却没有任何反应。 Here is my code: 这是我的代码:

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);
        }
    }
}

The problem is that you're setting drawRect1.Y property in the Update(...) method but then again you're overriding this rectangle in your Draw(...) method. 问题是,您要在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);

What you should do i to just update rectangle in your Update(...) method and use the same ( updated ) value in Draw(...) call. 我应该怎么做才能在Update(...)方法中更新矩形,并在Draw(...)调用中使用相同的(更新的)值。

To fix this just remove this line : drawRect1 = new Rectangle(0, WINDOW_HEIGHT - player1.Height, player1.Width, player1.Height); 要解决此问题,只需删除以下行: drawRect1 = new Rectangle(0, WINDOW_HEIGHT - player1.Height, player1.Width, player1.Height); from your Draw(...) method. 从您的Draw(...)方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM