2012-05-29 47 views
5

我有一個函數,爲給定另一個圖塊的特定圖塊設置繪圖狀態。繪製狀態的圖塊將更改周圍的圖塊,然後相應更新。我試圖說明如下有沒有辦法擺脫返回void的函數?

[b] [b] [a] 
[b] [a] [a] 
[a] [a] [a] where a = sand && b = water 

當a檢測到b與其相鄰時,它必須更新其繪製狀態。所以我有一個適用於大寫,小寫,左大小寫和右大寫的函數。我現在需要修改功能,因此,它可以處理左右的情況下,右上的情況下,右下的情況下,等等,等等這是我的功能

public override void CompareBorderingTiles(Tile T) 
    { 
     if (T is Water) 
     { 
      float leftBound = location.X - (Tile.TileWidth * Tile.TileScale); 
      float rightBound = location.X + (Tile.TileWidth * Tile.TileScale); 
      float upperBound = location.Y - (Tile.TileHieght * Tile.TileScale); 
      float bottomBound = location.Y + (Tile.TileHieght * Tile.TileScale); 
      if (T.GridLocation.X == leftBound) 
      { 
       drawstate = DrawState.Left; 
      } 
      if (T.GridLocation.X == rightBound) 
       drawstate = DrawState.Right; 
      if (T.GridLocation.Y == upperBound) 
       drawstate = DrawState.Upper; 
      if (T.GridLocation.Y == bottomBound) 
       drawstate = DrawState.Lower; 
     } 

     base.CompareBorderingTiles(T); 
    } 

它應該是相當解釋,爲什麼我想打破這個功能,或者不是。基本上我有一個枚舉,告訴我什麼是draw-state(drawstate是enum)。 有人可以告訴我,如果我可以設置正確的繪製狀態,然後離開我的功能?

+3

你意味着你想停止你的功能?如果這是真的,你可以使用'return;'爲它 –

回答

17

要結束只需使用一個return語句。

+0

我不知道我可以像這樣使用return關鍵字,謝謝。 –

7

只需使用return;就可以立即「返回」該功能。

雖然反思,但你真的需要返回?

if (T.GridLocation.X == leftBound) 
    { 
     drawstate = DrawState.Left; 
    } 
    else if (T.GridLocation.X == rightBound) 
    { 
     drawstate = DrawState.Right; 
    } 

    else if (T.GridLocation.Y == upperBound) 
    { 
     drawstate = DrawState.Upper; 
    } 
    else if (T.GridLocation.Y == bottomBound) 
    { 
     drawstate = DrawState.Lower; 
    } 

這應該會使代碼在未來更容易維護。

return; 

所以,在你的代碼,你可以這樣做:

public override void CompareBorderingTiles(Tile T) 
{ 
    if (T is Water) 
    { 
     float leftBound = location.X - (Tile.TileWidth * Tile.TileScale); 
     float rightBound = location.X + (Tile.TileWidth * Tile.TileScale); 
     float upperBound = location.Y - (Tile.TileHieght * Tile.TileScale); 
     float bottomBound = location.Y + (Tile.TileHieght * Tile.TileScale); 
     if (T.GridLocation.X == leftBound) 
     { 
      drawstate = DrawState.Left; 
      return; 
     } 
     if (T.GridLocation.X == rightBound) 
     { 
      drawstate = DrawState.Right; 
      return; 
     } 
     if (T.GridLocation.Y == upperBound) 
     { 
      drawstate = DrawState.Upper; 
      return; 
     } 
     if (T.GridLocation.Y == bottomBound) 
     { 
      drawstate = DrawState.Lower; 
      return; 
     } 
    } 

    base.CompareBorderingTiles(T); 
} 
+0

卡爾擊敗了競爭2秒:) – Brady

+0

大聲笑它會更快,如果它不是最少30個字符! :) –

+0

是的,我相信我應該因爲我需要根據瓷磚在我的網格上的位置來設置左上角狀態,左下角狀態等。 –

7

您可以return退出功能

+0

啊我不知道你可以使用自己的return關鍵字,我現在感覺像個白癡,謝謝。 –

3

您可以在函數中的任意位置使用return;,它將在此處保留函數。使用返回將意味着您的基本功能將被調用而不是。如果您需要基本函數的調用使用else if然後,當你條件滿足,就不會檢查剩餘的if語句:

public override void CompareBorderingTiles(Tile T) 
    { 
     if (T is Water) 
     { 
      float leftBound = location.X - (Tile.TileWidth * Tile.TileScale); 
      float rightBound = location.X + (Tile.TileWidth * Tile.TileScale); 
      float upperBound = location.Y - (Tile.TileHieght * Tile.TileScale); 
      float bottomBound = location.Y + (Tile.TileHieght * Tile.TileScale); 
      if (T.GridLocation.X == leftBound) 
      { 
       drawstate = DrawState.Left; 
      } 
      else if (T.GridLocation.X == rightBound) 
       drawstate = DrawState.Right; 
      else if (T.GridLocation.Y == upperBound) 
       drawstate = DrawState.Upper; 
      else if (T.GridLocation.Y == bottomBound) 
       drawstate = DrawState.Lower; 
     } 

     base.CompareBorderingTiles(T); 
    } 
+0

這個 - 即使你不想調用'base'函數,只是把它包裝在其他地方。這些'else if's比許多''返回''點綴更容易遵循。 – Rawling

+0

你擊敗了我2秒 –

0

使用return; 函數會返回一個空

相關問題