2013-10-30 60 views
1

我已經爲此搜索了堆棧溢出,並且我找到了一個有衝突的答案並且沒有直截了當的答案。你可以通過傳入一個類來改變它的值嗎?

這是在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; 
} 
+0

Vector2是一個結構體,因此按值傳遞,而不是通過引用傳遞。方法結束後,對傳遞給方法的任何更改都會丟失。改爲通過引用傳遞位置。 –

+0

@S_F:*如果參數不是'ref'或'out',則所有*都會被值傳遞。 – newacct

回答

相關問題