它显示的数组中的项目多于预期

时间:2016-12-22 12:57:15

标签: c# arrays xna

它显示的次数多于预期,当我添加1项并且它是可堆叠的时,它会添加到堆栈....但不会一次2次。

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArenaRPG
{
    public class Inventory
    {

        int slotWight;
        int slotHeight;
        int ItemsPerpage = 1;
        int CurrentPage;
        int Columns, Rows;

        B_Item[,] items;

        Backpack backpack;
        Vector2 pos;

        public Inventory(Backpack bpack,int itemsPerPage,int Columns,int Rows,  int SlotWidth, int SlotHeight, Vector2 Pos)
        {
            this.backpack = bpack;
            this.slotHeight = SlotHeight;
            this.slotWight = SlotWidth;
            this.pos = Pos;
            this.Columns = Columns;
            this.ItemsPerpage = itemsPerPage;
            this.Rows = Rows;
           items = new B_Item[Columns, Rows];
            LoadItems(bpack);                
        }

        public void Update(GameTime gameTime)
        {
            double Maxpages = Math.Ceiling((double)(items.Length - 1) / ItemsPerpage);

            if (Input.KeyPressed(Keys.A))
            {
                CurrentPage++;
            }

            if (Input.KeyPressed(Keys.D))
            {
                CurrentPage--;
            }

            Input.Update(gameTime);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            var result = items.Cast<B_Item>().Skip(ItemsPerpage * (CurrentPage-1)).Take(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);
                            if (items[X,Y].StackSize > 1)
                            {

spriteBatch.DrawString(AssetManager.GetInstance()。font [“Arial8”],items [X,Y] .StackSize.ToString(),new Vector2(DrawX + 20,DrawY + 20),Color.DarkBlue);                                 }                                 spriteBatch.DrawString(AssetManager.GetInstance()。font [“Arial8”],CurrentPage.ToString(),new Vector2(50,50),Color.DarkBlue);                             }

                    }
                }
            }
        }

        public void LoadItems(Backpack BPack)
        {
            int itemIndex = 0;

            for (int X = 0; X < Columns; X++)
            {
                for (int Y = 0; Y < Rows; Y++)
                {
                    if (itemIndex < BPack.items.Count)
                    {
                        items[X, Y] = BPack.items[itemIndex];
                        itemIndex++;
                    }
                }
            }
        }
    }
}

背包很简单

public void AddItems(B_Item NewItem)
{
    if (items.Count < MaxSlots)
    {
        foreach (var i in items.ToArray())
        {
            if (i.Item_Name == NewItem.Item_Name )
            {
                if (i.Is_Stackable == true && i.StackSize < 500)
                {
                    i.StackSize += 1;
                }
            }
            else
            {
                items.Add(NewItem);
            }
        }

    }
    else
    {
        items.Add(NewItem);
    }                   
}

private void Remove(B_Item itemID)
{

    for (int i = 0; i < items.Count; i++)
    {
        if (items.Count > 0)
        {
            items[i].StackSize -= 1;
        }

            items.RemoveAt(i);
    }
}

为什么在库存中绘制的项目数量超过值itemsPerPage指定的数量。 构造函数

Inventory thiss = new Inventory(new PlayerBack(),2,1,32,32, 32, new Vector2(0, 0))

1 个答案:

答案 0 :(得分:1)

好的,对不起评论这是错误的。这是正确的方法:

public void AddItems(B_Item NewItem)
        {
            if (items.Count < MaxSlots)
            {
                foreach (var i in items.ToArray())
                {
                    if (i.Item_Name == NewItem.Item_Name )
                    {
                        if (i.Is_Stackable == true && i.StackSize < 500)
                        {
                            i.StackSize += 1;
                            return;
                        }
                    }

                }

            }
            items.Add(NewItem);

        }