7

我有一个函数可以为给定另一个图块的特定图块设置绘制状态。绘制状态将发生变化的图块会比较它周围的图块,然后相应地更新。我将在下面尝试说明

[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);
    }

对于我为什么要突破这个功能,或者可能不这样做,这应该是很容易解释的。基本上我有一个枚举,它告诉我我的绘制状态是什么(绘制状态是枚举)。谁能告诉我是否可以设置正确的绘制状态然后退出我的功能?

4

5 回答 5

19

只需在要结束的地方使用 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);
}
于 2012-05-29T08:42:25.763 回答
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; 
    }

这应该使代码在将来更容易维护。

于 2012-05-29T08:42:23.173 回答
7

您可以使用 退出函数return

于 2012-05-29T08:42:44.030 回答
3

您可以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);
    }
于 2012-05-29T08:46:23.717 回答
0

使用return; 该函数将作为 void 返回

于 2012-05-29T08:48:35.940 回答