C# MonoGame:使用 Xbox 360 GamePad 上下移动 Sprite 图像
C# MonoGame: Moving Sprite Image Up and Down Using Xbox 360 GamePad
这是我的 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);
}
}
}
问题是您在 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);
。
这是我的 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);
}
}
}
问题是您在 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);
。