Foreach loop causing lag?

試著忘記壹切 提交于 2019-12-24 16:42:33

问题


In my previous question here. I successfully found out how to read and draw a level each frame from an image.

Now that I have inputted a Player class, the player moves really choppy despite the code not doing so. I know this because if I comment the level reading code out, the player moves fine.

I think it's the foreach loop doing this. I have no idea and help would be appreciated!

Just to save you time from clicking, here's my successful level-reading code.

public void readLevel(string path, GraphicsDevice graphics)
{

    //GET AN ARRAY OF COLORS
    Texture2D level = Content.Load<Texture2D>(path);
    Color[] colors = new Color[level.Width * level.Height];
    level.GetData(colors);

    //READ EACH PIXEL AND DRAW LEVEL
    Color brickRGB = new Color(128, 128, 128);
    Color blankRGB = new Color(87, 0, 127);

    int placeX = 0;
    int placeY = 0;

    foreach (Color pixel in colors)
    {
        SpriteBatch spriteBatch = new SpriteBatch(graphics);
        spriteBatch.Begin();

        if (pixel == brickRGB)
        {
            Texture2D brick = Content.Load<Texture2D>("blocks/brick");
            spriteBatch.Draw(brick, new Rectangle(placeX, placeY, 40, 40), Color.White);

            Rectangle rect = new Rectangle(placeX, placeY, 40, 40);
            blocks.Add(rect);
        }
        else if (pixel == blankRGB)
        {
            Texture2D back = Content.Load<Texture2D>("titlescreen/back");
            spriteBatch.Draw(back, new Rectangle(placeX, placeY, 40, 40), Color.White);
        }

        if (placeX == 840)
        {
            placeX = 0;
            placeY += 40;
        }
        else placeX += 40;
        spriteBatch.End();
    }
}

Game.cs:

class Game
{
    Player player;
    LevelReader reader;
    int level = 1;

    public Game(ContentManager content)
    {
        reader = new LevelReader(content);
        player = new Player(content);
    }

    public void Update()
    {
        player.Update();
    }
    public void Draw(GraphicsDevice graphics)
    {
        reader.readLevel("levels/l" + level, graphics);
        player.Draw(graphics);

    }
}

回答1:


You might want to move the calls to spritebatch.Begin() and spritebatch.End() outside of your loop.

SpriteBatch spriteBatch = new SpriteBatch(graphics);
        spriteBatch.Begin();
foreach (Color pixel in colors)
    {


        if (pixel == brickRGB)
        {
            Texture2D brick = Content.Load<Texture2D>("blocks/brick");
            spriteBatch.Draw(brick, new Rectangle(placeX, placeY, 40, 40), Color.White);

            Rectangle rect = new Rectangle(placeX, placeY, 40, 40);
            blocks.Add(rect);
        }
        else if (pixel == blankRGB)
        {
            Texture2D back = Content.Load<Texture2D>("titlescreen/back");
            spriteBatch.Draw(back, new Rectangle(placeX, placeY, 40, 40), Color.White);
        }

        if (placeX == 840)
        {
            placeX = 0;
            placeY += 40;
        }
        else placeX += 40;

    }
spriteBatch.End();


来源:https://stackoverflow.com/questions/38575567/foreach-loop-causing-lag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!