繁体   English   中英

有没有办法打破一个返回void的函数?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM