1

昨日、XNA でビューポートを使用していくつかのことを行っていましたが、位置を変更するときに、使用しているスプライトがビューポートよりも速く移動する理由がわかりませんでした。さまざまな値の型 (int と float) に関係があるのではないかと感じましたが、これについて詳しく説明してくれる人はいますか?

これが私が使用していたコードです...

    Viewport myViewport;
    Texture2D t;
    SpriteFont f;
    Vector2 point = new Vector2(0, 0);  

    /// <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();
        Keys[] pressedKeys = Keyboard.GetState().GetPressedKeys();
        for (int i = 0; i < pressedKeys.Length; i++)
        {
            if (pressedKeys[i] == Keys.W)
            {
                point.Y--;
            }
            else if (pressedKeys[i] == Keys.A)
            {
                point.X--;
            }
            else if (pressedKeys[i] == Keys.S)
            {
                point.Y++;
            }
            else if (pressedKeys[i] == Keys.D)
            {
                point.X++;
            }
        }
        myViewport.X = (int)point.X;
        myViewport.Y = (int)point.Y;
        GraphicsDevice.Viewport = myViewport;
        // TODO: Add your update logic here

        base.Update(gameTime);
    }

    /// <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.White);
        spriteBatch.Begin();
        spriteBatch.Draw(t, new Vector2(point.X, point.Y), Color.White);
        spriteBatch.End();
        // TODO: Add your drawing code here

        base.Draw(gameTime);
    }
4

1 に答える 1

5

まず、Draw 関数でビューポートを設定する必要があります。次に、ビューポートの境界が常に画面上にあることを確認する必要があります。

とにかく、そのように動く理由は、SpriteBatch の座標系がViewport に関してクライアント空間にあるためです。

つまり、 によると、位置 (0,0)SpriteBatchは の左上隅ですGraphicsDevice.Viewport

これが、レンダリング位置を変更する 2 つの異なる操作を効果的に実行しているため、スプライトが予想の 2 倍の速度で移動する理由です。

于 2011-02-14T07:59:59.377 に答える