簡體   English   中英

XNA只繪制一次spritebatch

[英]xna draw spritebatch only once

我是xna編程的新手,我在這里需要一些幫助...

我構建了一個具有靜態背景的2D游戲。在當前屏幕上,背景圖像是左側的4個大圖像,而我需要在右側繪制16個小圖像(或多或少50x50)

因此,這是我的方法,用於更新左側16張圖像的位置。Update僅調用一次

private void letterLblsLocation() 
    {
        if (letterLbls.Count != 0)
        {
            letterlblposition = new Vector2(0f, 0f);
            lblvector = new Vector2(0f, 0f);
            int col = 1;
            int lblsize = ScreenManager.GraphicsDevice.Viewport.Height / 15;
            for (int lbl = 0; lbl < letterLbls.Count; lbl++)
            {
                letterlblposition.X = ScreenManager.GraphicsDevice.Viewport.Width - (col * lblsize);
                letterlblposition.Y += 5 + lblsize;
                if (letterlblposition.Y > ScreenManager.GraphicsDevice.Viewport.Height - lblsize)
                {

                    col = col + 3;
                    letterlblposition.Y = 5 + lblsize;
                }
                recsOnRight.Add(new Rectangle((int)letterlblposition.X, (int)letterlblposition.Y, lblsize, lblsize));

                lblvector.X = recsOnRight[lbl].X + 5;
                lblvector.Y = recsOnRight[lbl].Y;
                letterLbls[lbl].Position = lblvector;
            }
        }
    }

這是我的更新方法

public override void Update(GameTime gameTime, bool otherScreenHasFocus,bool coveredByOtherScreen)
    {
        base.Update(gameTime, otherScreenHasFocus, false);



        // Gradually fade in or out depending on whether we are covered by the pause screen.
        if (coveredByOtherScreen)
            pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1);
        else
            pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0);

        if (IsActive)
        {
            if(questionNeeded)
            {
                ChooseWord();
                ChooseLettersOnRight();
                LabelsWithLetters();
                letterLblsLocation();
            }


        }
    }

這是我的Draw方法

public override void Draw(GameTime gameTime)
    {

        spriteBatch.Begin();
        spriteBatch.Draw(background, new Rectangle(0, 0, ScreenManager.GraphicsDevice.Viewport.Width, ScreenManager.GraphicsDevice.Viewport.Height), Color.White);
        if (!questionNeeded)
        {
            for (int i = 0; i < 16; i++)
            {
                if (i < 4)
                {
                    Texture2D helpTexture = questionToDisplay.pic[i];
                    Rectangle helpRect = questionToDisplay.picRecs[i];
                    spriteBatch.Draw(helpTexture, helpRect, Color.White);
                }
                spriteBatch.Draw(labelSquare, recsOnRight[i], Color.White);
                }
            }

        spriteBatch.End();

現在我的問題是,當我嘗試在右側繪制16個spritebatch時,游戲會變慢是否有辦法只繪制一次16張圖片並僅在玩家單擊其中的一張時重繪它們? 還是合並它們以便繪制一個大圖像而不是16個小圖像的方法? 或者由於我在這里的經驗很少,因此在編寫這些圖片時,此代碼中是否有任何必須更改的內容會導致游戲運行緩慢?

提前致謝

您將在Draw調用期間初始化圖形設備,SpriteBatch,SpriteFont和紋理。 這真的很糟糕。

將這些初始化移到Draw函數之外的LoadContent()函數中,並且只初始化一次。

好吧,我只是從draw方法中刪除了for循環,然后調用了spriteBatch.Draw 16次,每個sprite一個。 是的,顯然這不是最佳解決方案,但它確實有效,並且速度再次恢復正常。 另外,正如喬恩提到的那樣,我將所有初始化都從Draw或Update中移了出來,然后才使代碼更快。

您的代碼段中沒有任何地方可以大大降低應用程序的速度。 嘗試使用秒表測試Draw和Update方法。 使用fps 60(默認情況下)時,總計不應超過1/60秒。 嘗試本地化真正減慢游戲速度的部分。

只在不正確的情況下繪制圖像呢? XNA不是WinForms,並且Draw()調用每個幀,您應該清除屏幕並重新繪制要顯示給播放器的所有內容。 游戲循環很簡單:一次調用Initialize()和LoadContent,然后在“無限”循環中以一定頻率調用Update和Draw,最后一次調用UnloadContent。

如果要在單擊后更改圖像,則需要具有兩個圖像-單擊和未單擊,以及一些變量以保持每個圖像的狀態。 您可以在Update方法中檢查鼠標/鍵盤輸入,更改變量狀態,然后在Draw方法中確定傳遞給spritebatch.Draw()調用的圖像。 另外,如果您有一個包含很多精靈的復雜場景,則可以在每次更新后只繪制一次渲染目標,然后像實心紋理一樣繪制它(“很多”的意思是100s,例如Terraria,但不是16個藍色矩形) 。

暫無
暫無

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

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