【发布时间】:2012-09-15 18:01:34
【问题描述】:
我正在制作一个库存系统,但我被困在应该通过简单的拖放将项目从一个单元格移动到另一个单元格的部分。
有一个 Item[,] Inventory 数组保存项目,object fromCell, toCell 应该保存对单元格的引用,以便在释放鼠标按钮时进行操作,但是当我尝试这样做时:
object temp = toCell;
toCell = fromCell;
fromCell = temp;
...游戏只是交换对象引用而不是实际对象。我该如何完成这项工作?
UPD:感谢 Bartosz,我明白了这一点。事实证明,您可以安全地使用对对象数组的引用,并使用保存的您希望交换的对象的索引来更改 it。
代码可以是这样的:
object fromArray, toArray;
int fromX, fromY, toX, toY;
// this is where game things happen
void SwapMethod()
{
object temp = ((object[,])toArray)[toX, toY];
((object[,])toArray)[toX, toY] = ((object[,])fromArray)[fromX, fromY];
((object[,])fromArray)[fromX, fromY] = temp;
}
【问题讨论】: