How skip and take in array

1.9k views Asked by At

Basically what I want to do is show 16 items to screen and extra ones add to new page.

16 items are are shown per page and extra by 16 are separated ob other screens.

I'm using 2D array to display/show to screen

Vector2 Pos;
int ItemsPerpage = 16;
int CurrentPage;

public void Draw(SpriteBatch spriteBatch)
{
        var result = items.Cast<double>().Skip(ItemsPerpage * CurrentPage).Skip(ItemsPerpage);

        foreach (var item in items)
        {
            for (int X = 0; X < Columns; X++)
            {
                for (int Y = 0; Y < Rows; Y++)
                {
                    int DrawX = (int)pos.X + (X * (slotWight + 2));
                    int DrawY = (int)pos.Y + (Y * (slotWight + 2));

                    if(items[X,Y] != null)
                    {
                        spriteBatch.Draw(items[X,Y].Texure, new Rectangle(DrawX, DrawY ,32, 32), new Rectangle(0, 0, 64, 64), Color.White);
                    }
                }
            }
        }
    }

In this code spritebatch.draw(item.texture dose not exit) if I replace in for each loop items with result.

Example

public void Draw(SpriteBatch spriteBatch)
{
        var result = items.Cast<double>().Skip(ItemsPerpage * CurrentPage).Skip(ItemsPerpage);
        foreach (var item in result)
        {
            for (int X = 0; X < Columns; X++)
            {
                for (int Y = 0; Y < Rows; Y++)
                {
                    int DrawX = (int)pos.X + (X * (slotWight + 2));
                    int DrawY = (int)pos.Y + (Y * (slotWight + 2));
                    if(items[X,Y] != null)
                    {
                        spriteBatch.Draw(item.Texure, new Rectangle(DrawX, DrawY ,32, 32), new Rectangle(0, 0, 64, 64), Color.White);
                    }

                }
            }
        }
    }

Here it show me a all items in the list. As show in this picture a busy cat

1

There are 1 answers

11
mybirthname On BEST ANSWER
 var result = items.Cast<double>().Skip(ItemsPerpage * (CurrentPage-1)).Таке(ItemsPerpage);

First problem you are not using Take. Second problem in your skip you should have itemsPerPage * (CurrentPage-1), because if you are on first page you should not skip any records so: (1-1)*ItemsPAerPage = 0.