2016-06-07 52 views
-2

我在製作遊戲。物體落在這場比賽中。我打算通過交換2D數組中的元素來使這個「下降」發生。每個元素都是一個保存顏色的對象。array2D中的交換元素java

我搜索所有元素。

code in here

應該互換。

我不明白我在做什麼錯。

糾正我,如果我錯了,但不是這樣交換嗎?

object A, 

object B, 

temp c = B, 

B = A, 

A = c, 
+3

發佈[mcve]併發布_here_,所以代碼在問題本身。 – Tom

+0

發佈問題中的代碼;沒有鏈接。我沒有看到鏈接中發生任何交換。 – duffymo

+0

請停止寫作並說「基本」和「如此」。 – duffymo

回答

0

我會後的relevand部分從您的代碼,並回答它

 Int x = currentTile.getX()/50; 
     int y = (currentTile.getY()+1)/50; 

     CellPane botTile = board[x][y]; 
     if (botTile.getBackground().equals(Color.BLACK)){ 
      board[x][y].setBackground(currentTile.getBackground()); 
      board[currentTile.getX()/50][currentTile.getY()/50].setBackground(botTile.getBackground()); 
     } 

botTile保持作爲板[X] [Y]的參考同一對象(板片)。您檢查botTile的顏色並將其替換爲currentTile上的顏色。這意味着botTile和board [x] [y]引用的對象現在具有新的顏色。這意味着舊的顏色丟失了,因爲你沒有將它保存在另一個變量中。你應該做這樣的事情

 CellPane botTile = board[x][y];   
     if (botTile.getBackground().equals(Color.BLACK)){ 
      Color oldColor = botTile.getBackground; 
      board[x][y].setBackground(currentTile.getBackground()); 
      board[currentTile.getX()/50][currentTile.getY()/50].setBackground(oldColor); 
     } 
0

既然你劃分值與50 y值不會增加,除非值是50,100等等... Ÿ分配如下:

int y = 1 + currentTile.getY()/50;