1

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • You want to skip and take, but your code you are just using `Skip`. I imagine the second `Skip` is supposed to be a `Take`. – Abion47 Dec 22 '16 at 10:20
  • Also, the error you are getting is because you are casting `items` into a collection of type `double` before you operate on them, and `double` doesn't have a field named `Texture`. Any reason for doing the cast? – Abion47 Dec 22 '16 at 10:22
  • I just did in examples.. just it shows same amount as before. And `item.texture` can't be seen if i replace items list with result. `spriteBatch.Draw(texture,rectangle(int,int,int,int),Color.White);` i need to set texture what to draw....in first parameter. I did set in first rectangle where to draw and what side, second rectangle is used what part of picture to draw and where. So if i had spritesheet i can use specific sprite to draw using second rectangle. – user3768433 Dec 22 '16 at 10:23
  • Well i was thinking since it was 2D to to make it 1 dimentional – user3768433 Dec 22 '16 at 10:24
  • When i replaced double with Texture2D it gave me error Instance is null at item.texutre. Even texture is declared in my Backpack. – user3768433 Dec 22 '16 at 10:27
  • @Abion47 Thanks for that fix, it never occured to me xD – user3768433 Dec 22 '16 at 10:32
  • What *is* the type of `items`? You are accessing it like a 2D array, but you want to do `Skip`/`Take` on it? I feel like your design needs to be changed somewhere. – Abion47 Dec 22 '16 at 10:32

1 Answers1

2
 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.

mybirthname
  • 17,949
  • 3
  • 31
  • 55
  • I did use..i was typing in here...same then i forgot to change it. Still... that was one of the bugs. But part of items.texture = null... In PlayerBackpack there is item that has declared texure and what it is...how come i get now a null? – user3768433 Dec 22 '16 at 10:30
  • @user3768433 The policy of SO is to ask one question per post. You can ask new question about it, but probably it will be closed because of duplicate. Check this question: http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – mybirthname Dec 22 '16 at 10:34
  • This assumes that the pages are listed with `1` as the first page. If the first page is represented as `0`, then this code will get the wrong page. – Abion47 Dec 22 '16 at 10:34
  • @Abion47 I never seen pages to be represent as 0 in such way we can say that page can be represent from -1, why not ? Also you can see his screenshot Page 1/6, so my assumption is correct. – mybirthname Dec 22 '16 at 10:36
  • In the UI, it is very common for pages to start at 1, as this would be more intuitive for the end user. In code, however, it is more common for the first page to be represented as 0 than as 1. This makes it much easier and cleaner to treat them as indices for an array or to do calculations using them (as is the case here). – Abion47 Dec 22 '16 at 10:39
  • @Abion47 the parameters of this query are coming from gui, so it will be not very wise to decrease them with one for some reason. I will say that is not common at all. – mybirthname Dec 22 '16 at 10:40
  • `The parameters of this query are coming from gui` If that is the case then the code is badly designed. The logic/data should *never* be coupled to the GUI. Read up on [MVVM architecture](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel). – Abion47 Dec 22 '16 at 10:45
  • Well for first page it must start at 0...my mistake. Cuz i need to show first page not and later i need to display as page 1. not page. Currently how i see for page 1 would be. Skip0) since itemsperpage * current page - 0, So it will not skip first page. Then take(itemsperpage) will display first 20 items...as example. Then Aftet each increasse in currentpage it will take Skip(itemsPerpage(20) * CurrentPage(2)) = 20 And Take will start form index 21... – user3768433 Dec 22 '16 at 10:46
  • @Abion47 I'm not doin in gui..XNA – user3768433 Dec 22 '16 at 10:46
  • @user3768433 If your program has a window that the user can see and interact with in any way, then you have a GUI. – Abion47 Dec 22 '16 at 10:50
  • @Abion47 it's a game..so yea it has – user3768433 Dec 22 '16 at 11:11