2014-03-25 55 views
1

如何從單元格[5,4]中減去單元格[3,5]?我有一個單元格對象,它創建一個二維數組並在其中設置戰士對象。 我試過,但它因此未工作減去對象的數組值

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

我的手機類是這樣的:

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

你是什麼意思,'我試過這個但是id沒有工作?'你能解釋一下嗎? –

+0

你有一個'Cell(Warrior warrior)類型的構造函數嗎?'? –

+0

公共細胞(戰士getWarrior) { this._warrior = getWarrior; }我正在設置這樣的戰士 – Grande

回答

3

我寫的示例代碼嘗試這樣的..

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

+0

感謝這項工作 – Grande

+0

@PushpitaShrestha:歡迎。 –

+0

這工作。現在我想獲得已經設置爲單元格的戰士[3,5]。我做了細胞[3,5] ._戰士,但它不會返回任何東西 – Grande