![](/img/trans.png)
[英]SelectedIndex returns -1 in void/static void function
[英]Is there any way to break out of a function that returns void?
我有一個函數,為給定另一個tile的特定tile設置繪制狀態。 繪制狀態將更改的圖塊比較周圍的圖塊然后相應地更新。 我將嘗試在下面說明
[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);
}
對於為什么我想要突破這個功能,或者可能不是這個功能,它應該是非常明確的解釋。 基本上我有一個枚舉,告訴我我的繪制狀態是什么(drawstate是枚舉)。 誰能告訴我是否可以設置正確的繪制狀態然后退出我的功能?
只需使用您想要結束的return語句:
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);
}
只需使用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
退出函數。
你可以使用return;
在函數的任何一點,它將在那里離開函數。 使用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);
}
使用return;
該函數將作為void返回
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.