1

我已经为此搜索了 Stack Overflow,我发现了一个答案相互矛盾的答案,没有任何答案是直截了当的。

这是在 Microsoft XNA 中。Vector2 是一个可以容纳 X 和 Y 的类。该函数查看其方向上的正方形是否为空。如果是,请朝相应的方向移动。NORTH 函数将位置的 y 值减 1。它将 2 添加到矢量,但它不会转移到屏幕本身并移动该块。问题是我不能像这样编辑向量的值,我需要返回它并以这种方式设置吗?

public static Boolean domove(Vector2 location, int[,] board, char dir) //won't work, I cant edit it's values like this.
{
    if (dir == 'N')
    {
        if (board[(int)location.X, (int)location.Y - 1] == 1) { NORTH(location); Console.WriteLine(location); return true; } //if the square ahead, which would be a potential wall, is 0, or unoccupied, hop forward 2. go into technicalities of another player there later.
    }
    else if (dir == 'S')
    {
        if (board[(int)location.X, (int)location.Y + 1] == 1) { SOUTH(location);  return true; } //if the square ahead, which would be a potential wall, is 0, or unoccupied, hop forward 2. go into technicalities of another player there later.
    }
    else if (dir == 'W')
    {
        if (board[(int)location.X - 1, (int)location.Y] == 1) { WEST(location); return true; } //if the square ahead, which would be a potential wall, is 0, or unoccupied, hop forward 2. go into technicalities of another player there later.
    }
    else if (dir == 'E')
    {
        if (board[(int)location.X + 1, (int)location.Y] == 1) { EAST(location); return true; } //if the square ahead, which would be a potential wall, is 0, or unoccupied, hop forward 2. go into technicalities of another player there later.
    }
    return false;
}
4

1 回答 1

2

您必须通过引用传递该值才能在其上写入而无需再次返回。请参阅有关 ref 关键字的 C# 参考文档:http: //msdn.microsoft.com/en-us/library/14akc2c7.aspx

于 2013-10-30T23:18:09.940 回答