[英]Is there any way to break out of a function that returns void?
I have a function that sets a draw-state for a specific tile given another tile. 我有一个函数,为给定另一个tile的特定tile设置绘制状态。 The tile that's draw state is going to change compares tiles that are surrounding it and then updates accordingly.
绘制状态将更改的图块比较周围的图块然后相应地更新。 I'll try to illustrate that below
我将尝试在下面说明
[b] [b] [a]
[b] [a] [a]
[a] [a] [a] where a = sand && b = water
when a detects that b is bordering it, it must update its draw-state. 当a检测到b正在接近它时,它必须更新其绘制状态。 So I have a function that works for an upper case, lower case, left case, and right case.
所以我有一个适用于大写,小写,左大小写和右大小写的函数。 I now need to modify that function so it can handle a left-right case, upper-right case, lower-right case, etc. etc. Here is my function
我现在需要修改该功能,以便它可以处理左右情况,右上角情况,右下角情况等。这是我的功能
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);
}
It should be pretty explanatory as to why i'd want to break out of this function, or maybe not. 对于为什么我想要突破这个功能,或者可能不是这个功能,它应该是非常明确的解释。 Basically I have an enum which tells me what my draw-state is (drawstate is the enum).
基本上我有一个枚举,告诉我我的绘制状态是什么(drawstate是枚举)。 Can anybody tell me if I can set that correct draw state and then get out of my function?
谁能告诉我是否可以设置正确的绘制状态然后退出我的功能?
Just use a return statement where you want to end: 只需使用您想要结束的return语句:
return;
So, in your code you could do: 所以,在您的代码中,您可以这样做:
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);
}
Just use return;
只需使用
return;
on its own, this will immediately "return" from the function. 就其本身而言,这将立即从函数“返回”。
On reflection though do you really need to 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;
}
That should make the code easier to maintain in the future. 这应该使代码在未来更容易维护。
您可以使用return
退出函数。
You could use return;
你可以使用
return;
at any point in the function and it will leave the function there and then. 在函数的任何一点,它将在那里离开函数。 Using return would mean your base function would not be called.
使用return意味着不会调用您的基函数。 If you require the base function to be called use
else if
then when you're condition is fulfilled it won't check the remaining if statements: 如果您需要基本函数被调用使用
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);
}
use return;
使用
return;
the function will return as a void 该函数将作为void返回
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.