减去对象的数组值



如何从单元格[5,4]中减去单元格[3,5]?我有一个单元格对象,它创建了一个2D数组,并在其中设置了战士对象。我试过这个,但它没有工作

Cell[,] cells = new Cell[6,6];
cells[3, 5] = new Cell(warrior);
cells[5,4] = new Cell(warrior);
...
int x1 = cells[3, 5].x - cells[5, 4].x;
int x2 = cells[3, 5].y - cells[5, 4].y;
Console.WriteLine(x1);
Console.WriteLine(x2);

我的Cell类是这样的:

public class Cell
{
    public int _x;
    public int _y;
    public Warrior _warrior; 
}

我已经写了一个示例代码,尝试像这样…

 Warrior warrior = new Warrior(25,24);
        Warrior warrior1 = new Warrior(20,20);
        Cell[,] cells = new Cell[6, 6];
        cells[3, 5] = new Cell(warrior);
        cells[5, 4] = new Cell(warrior1);
        int x1 = cells[3, 5]._x - cells[5, 4]._x;
        int x2 = cells[3, 5]._y - cells[5, 4]._y;
        Console.WriteLine(x1);
        Console.WriteLine(x2);
 public class Cell
{
    public Cell(Warrior warrior)
    {
        _x = warrior.x;
        _y = warrior.y;
    }
    public int _x;
    public int _y;
    public Warrior _warrior;
}
public class Warrior
{
    public Warrior(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
    public int x;
    public int y;
}

输出:5 4

相关内容

  • 没有找到相关文章

最新更新