为什么对象在键盘上不移动 右/左键按XNA
本文关键字:XNA 移动 对象 键盘 为什么 | 更新日期: 2023-09-27 18:34:17
我正在创建一个简单的XNA游戏,您可以在其中移动块。但是块在按键时没有移动,我不知道为什么。
这是我的整体代码:
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D ball;
Texture2D bar;
Vector2 ballPos;
Vector2 barPos;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
// Frame rate is 30 fps by default for Windows Phone.
TargetElapsedTime = TimeSpan.FromTicks(333333);
// Extend battery life under lock.
InactiveSleepTime = TimeSpan.FromSeconds(1);
}
/// <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
ballPos = new Vector2(10, 10);
barPos=new Vector2(10,GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width - 20);
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()
{
ball = Content.Load<Texture2D>("ball");
bar = Content.Load<Texture2D>("bar");
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all 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)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
UpdateBarLocation();
// TODO: Add your update logic here
base.Update(gameTime);
}
private void UpdateBarLocation()
{
KeyboardState newState = Keyboard.GetState();
if (newState.IsKeyDown(Keys.Right) && (barPos.Y < GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height))
barPos.Y = barPos.Y + 5;
else if (newState.IsKeyDown(Keys.Left) && (barPos.Y > 2))
barPos.Y = barPos.Y - 5;
}
/// <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);
// TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.Draw(ball,ballPos, Color.White);
spriteBatch.Draw(bar, barPos, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
您可以看到,在 updateBarLocation 函数中,我正在尝试移动栏,但它不起作用。
屏幕的分辨率是多少?
这是行是模棱两可的
barPos=new Vector2(10,GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width - 20);
第一个参数是 x 轴,第二个参数是 y 轴。所以宽度通常在x轴上处理。您确定要分配的内容没问题吗?如果是,那么酒吧最初位于屏幕上的什么位置?
但是块在按键时没有移动,我不知道为什么。
你甚至没有更新块的位置