2016-12-22 67 views
-1

它顯示更多的時間比預期,當我添加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()。字體[ 「Arial8」],項[X,Y] .StackSize.ToString(),新Vector2(DrawX + 20,+ DrawY 20),顏色。深藍); } 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)) 
+0

您添加相同的項目2次,刪除AddItems方法結束時的'items.Add(NewItem)'。 – Shakra

+0

刪除2nd items.Add(NewItem)現在它不顯示任何項目到屏幕上。 – user3768433

回答

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); 

     }