为什么对象没有移动键盘右/左键按XNA

时间:2012-05-16 14:54:34

标签: c# windows-phone-7 xna

我正在创建一个简单的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函数中我试图移动栏但它不起作用。

1 个答案:

答案 0 :(得分:0)

屏幕的分辨率是多少?

这是不明确的行

barPos=new Vector2(10,GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width - 20);

第一个参数是x轴,第二个参数是y轴。 所以宽度通常用x轴处理。 你确定你指的是好吗? 如果是,那么屏幕最初位于哪个位置?

  

但是这个块并没有按下按键,我不知道为什么。

你甚至没有更新Block的位置