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 軸で、2 番目のパラメータは y 軸です。したがって、幅は通常 x 軸で扱われます。あなたが割り当てているものは大丈夫ですか?はいの場合、画面上のどこにバーが最初に配置されていますか?

しかし、キーを押してもブロックが動かないのはなぜですか。

ブロックの位置を更新していません

于 2012-05-16T16:13:48.253 に答える