0

ウィンドウに 2 つのボリューム アイコンを描画することになっているこのコードがありますが、機能していません。関連するコードは次のとおりです。

    Texture2D vol_max;
    Vector2 vol_max_vect;
    Texture2D vol_min;
    Vector2 vol_min_vect;
    ...
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        vol_max = Content.Load<Texture2D>("vol_max@16");
        vol_min = Content.Load<Texture2D>("vol_min@16");
    }
    protected override void Update(GameTime gameTime)
    {
        thisKeyboard = Keyboard.GetState(PlayerIndex.One);

        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
            thisKeyboard.IsKeyDown(Keys.Escape))
        {
            this.Exit();
        }

        // Update window vectors
        vol_max_vect = new Vector2(
            (float)(Window.ClientBounds.Right - 20),
            (float)(Window.ClientBounds.Bottom - 20));
        vol_min_vect = new Vector2(
            (float)(Window.ClientBounds.Right - 140),
            (float)(Window.ClientBounds.Bottom - 20));

        prevKeyboard = thisKeyboard;

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();
        spriteBatch.Draw(
            vol_max,
            vol_max_vect,
            Color.White);
        spriteBatch.Draw(
            vol_min,
            vol_min_vect,
            Color.White);
        spriteBatch.End();

        base.Draw(gameTime);
    }
4

1 に答える 1

2

問題は、ClientBounds.Right/Bottom が Windows 画面座標 ([0,0] は画面の左上、右下は [1024, 768] などの解像度) であることです。

本当に欲しいのは、自分のウィンドウの右下にそれらを描画することです。XNA の SpriteBatch はビューポート座標で描画します。[0, 0] はビューポートの左上、右下はアプリケーションの解像度です。[800、480]。その幅を取得するには、Window.ClientBounds.Right の代わりに Window.ClientBounds.Width を使用し、Window.ClientBounds.Bottom の代わりに Window.ClientBounds.Height を使用します。

うまくいけば、それは役に立ちます!

于 2012-05-13T03:45:54.253 に答える