簡體   English   中英

如何在xna中擺脫縱向不必要的黑色方塊?

[英]how can I get rid of the unnecessary black squares in portrait orientation in xna?

我有一個圖像,我設置它,以便支持的唯一方向是肖像使用:

graphics.SupportedOrientations = DisplayOrientation.Portrait; 在game1()方法上

如何擺脫屏幕上下邊緣的黑框?

這就是它的樣子:

在此輸入圖像描述

忽略那個29,這是我的fps監視器。

在改變方向之前:

在此輸入圖像描述

改變之后:

在此輸入圖像描述

這是我的代碼。 你會注意到很多數組和添加內容,我會用它來做別的事情。 現在,我並沒有真正使用它:

    Texture2D mineField;

    Vector2 mineFieldLocation,
        numFontLocation;

    int[] fieldPos = new int[9];

    float[]
        minePosX = new float[9],
        minePosY = new float[9];

    SpriteFont numFont;

    int posAdder = 45,
        index = 0;

    Random randMinePos = new Random();

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";

        graphics.SupportedOrientations = DisplayOrientation.Portrait;

       graphics.IsFullScreen = true;

        // 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


        // le sumo o resto a la x o y 45 para que caiga en el mismo medio del cuadrado
        mineFieldLocation = new Vector2(30, 70);

        numFontLocation = new Vector2(33, 65);

        for (int i = 0; i < fieldPos.Length; i++)
        {
            fieldPos[i] = posAdder;

            posAdder += 45;                
        }

        for (int i = 0; i < fieldPos.Length; i++)
        {
            minePosX[i] = fieldPos[randMinePos.Next(0, 9)];
            minePosY[i] = fieldPos[randMinePos.Next(0, 9)];
        }

        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()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        mineField = Content.Load<Texture2D>("Minesweeper");

        numFont = Content.Load<SpriteFont>("NumberFont");

        // 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();

        // 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.CornflowerBlue);

        spriteBatch.Begin();

        spriteBatch.Draw(mineField, mineFieldLocation, Color.White);

        spriteBatch.DrawString(numFont, "1", numFontLocation, Color.Green);

       // spriteBatch.DrawString(numFont, "0", new Vector2(minePosX[index],minePosY[index]), Color.Black);

        if (index < 8)
        {
            index++;
        }

        spriteBatch.End();

        // TODO: Add your drawing code here

        base.Draw(gameTime);
    }
}

正如我在評論中所說,你需要設置首選的寬度和高度,但我會解釋為什么..

首先, SupportedOrientations的文檔說明

獲取或設置啟用自動旋轉和縮放時可用的顯示方向。

我不相信你確實啟用了(不確定它是否默認)。 繼此之后還有一個參見此頁面上的自動旋轉和縮放鏈接,它提供了許多關於設置寬度和高度的提示......

如果你再看一下你的第三張圖片,那就有必要了,因為你當前的高度看起來像你的寬度,這讓我相信你現在的寬度現在是你之前的高度。 所以要確定我會設置兩者

在僅縱向模式下,您需要交換后緩沖區的寬度和高度值:

//For 480x800 (90* rotated wp7 device)
Graphics.PreferredBackBufferWidth = 480;
Graphics.PreferredBackBufferHeight = 800;

您正在480x800屏幕上渲染800x480后備緩沖區。 這導致它成為信箱。

也許你會感興趣:
XNA - 獲取當前屏幕分辨率
XNA:獲取屏幕的寬度和高度

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM