0

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

4

1 回答 1

0

屏幕分辨率是多少?

这是行不明确

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

第一个参数是 x 轴,第二个是 y 轴。所以宽度通常在x轴上处理。你确定你分配的东西没问题吗?如果是,那么屏幕上最初的位置是什么?

但是该块没有在按键上移动我不知道为什么。

你甚至没有更新 Block 的位置

于 2012-05-16T16:13:48.253 回答