【发布时间】:2013-12-19 13:14:25
【问题描述】:
我正在构建我的第一个库存,我希望它能够像任何其他基本库存一样工作:
- 当您捡起某样东西时,它会将其添加到您的库存中,供您以后装备。
- 它的外观和工作方式类似于狂神领域的物品栏。
我完成了一些基本的工作,例如:
- 绘制库存槽,当您将鼠标悬停时它们会改变纹理。
- 用鼠标移动项目/对象。
就是这样。
所以现在我陷入了涉及如何处理项目的部分,我知道有几种处理项目的方法,但我不知道在我的当前代码中最简单的方法是什么我正在使用。
如果有人能指出我正确的方向,我将非常感激,这将为我节省大量时间!
这是代码顺便说一句:
class InventorySlots
{
Texture2D box;
Texture2D blackbox;
Texture2D empty;
const int offSet = 100;
Rectangle[,] Inventoryslots = new Rectangle[6, 4];
Rectangle testrect; // Rect for moving item
bool isMoving;
public void LoadContent(ContentManager Content)
{
box = Content.Load<Texture2D>("Box");
blackbox = Content.Load<Texture2D>("boxselected");
testrect = new Rectangle(10, 20, box.Width, box.Height);//Set up my test rect
empty = box;
for (int x = 0; x < 6; x++)
{
for (int y = 0; y < 4; y++)
{
Inventoryslots[x, y] = new Rectangle((x * box.Width) + offSet, // Setup my inventory slots
(y * box.Height) + offSet, box.Width, box.Height);
}
}
}
public void Update(GameTime gameTime)
{
if ((ButtonState.Pressed == Mouse.GetState().LeftButton))
{
if (testrect.Intersects(new Rectangle(Game1.mousePosition.X, Game1.mousePosition.Y, 0, 0)))
{
isMoving = true;
}
}
else
{
isMoving = false;
}
if (isMoving)
{
testrect = new Rectangle(Game1.mousePosition.X - testrect.Width / 2, Game1.mousePosition.Y - testrect.Height / 2, testrect.Width, testrect.Height);
}
}
public void Draw(SpriteBatch spriteBatch)
{
for (int x = 0; x < 6; x++)
{
for (int y = 0; y < 4; y++)
{
if (Inventoryslots[x, y].Contains(Game1.mousePosition))
{
empty = box;
}
else if (!Inventoryslots[x, y].Contains(Game1.mousePosition))
empty = blackbox;
spriteBatch.Draw(empty, Inventoryslots[x, y], Color.White);
}
}
spriteBatch.Draw(box, testrect, Color.White);//Draw my test item that i can move around
}
}
【问题讨论】: