我正在尝试移动一个精灵,但它甚至不让我调试。
当我尝试调试时,我收到以下错误:
错误1当前上下文中不存在名称“键”
我做错了什么?或者是别的什么?根据多个来源,这是使精灵移动的方法之一。
我从来没有能够使用键盘输入移动某些内容或因某些原因发生某些事情。有点丢失在这里:(
以下是代码:
public class Game1 : Microsoft.Xna.Framework.Game
{
Texture2D myTexture;
Vector2 spritePosition = Vector2.Zero;
Vector2 spriteSpeed = new Vector2(50.0f, 50.0f);
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
KeyboardState keystate;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
myTexture = Content.Load<Texture2D>("sprite");
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
keystate = Keyboard.GetState();
if (keystate.IsKeyDown(keys.right))
spritePosition.X += spriteSpeed.X;
}
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(myTexture, spritePosition, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
答案 0 :(得分:1)
使用实际的XNA Keys
枚举(MSDN):
protected override void Update(GameTime gameTime)
{
keystate = Keyboard.GetState();
if (keystate.IsKeyDown(Keys.Right))
spritePosition.X += spriteSpeed.X;
}
请记住,C#区分大小写!