方法:并非所有代码路径都返回值

本文关键字:代码 路径 返回值 方法 | 更新日期: 2023-09-27 18:02:44

当我在开发一款XNA 4.0游戏时,我遇到了这个问题,我的一个方法是得到错误"不是所有的代码路径返回一个值",这让我在过去的几个小时里疯了。

 private Rectangle HandleCollision(Rectangle bounds, TileCollision collision, Rectangle tileBounds)
    {
        Vector2 depth = RectangleExtensions.GetIntersectionDepth(bounds, tileBounds);
        if (depth != Vector2.Zero)
        {
            float absDepthX = Math.Abs(depth.X);
            float absDepthY = Math.Abs(depth.Y);
            // Resolve the collision along the shallow axis.  
            if (absDepthY < absDepthX || collision == TileCollision.Platform)
            {
                // If we crossed the top of a tile, we are on the ground.
                // also ladder
                if (previousBottom <= tileBounds.Top)
                {
                    if (collision == TileCollision.Ladder)
                    {
                        if (!isClimbing && !isJumping)
                        {
                            //walking over a ladder
                            isOnGround = true;
                        }
                    }
                    else
                    {
                        isOnGround = true;
                        isClimbing = false;
                        isJumping = false;
                    }
                }

                // Ignore platforms, unless we are on the ground.  
                if (collision == TileCollision.Impassable || IsOnGround)
                {
                    // Resolve the collision along the Y axis.  
                    Position = new Vector2(Position.X, Position.Y + depth.Y);
                    // Perform further collisions with the new bounds.  
                    bounds = BoundingRectangle;
                }
            }
            else if (collision == TileCollision.Impassable) // Ignore platforms.  
            {
                // Resolve the collision along the X axis.  
                Position = new Vector2(Position.X + depth.X, Position.Y);
                // Perform further collisions with the new bounds.  
                bounds = BoundingRectangle;
            }
            else if (collision == TileCollision.Ladder && !isClimbing)
            {
                //stops colliding with ladder if player walks past or drops off ladder
                Position = new Vector2(Position.X, Position.Y);
                //perform collisions with new bounds
                bounds = BoundingRectangle;
            }
            return bounds;
        }
    }

方法:并非所有代码路径都返回值

你的问题在这里

if (depth != Vector2.Zero)

如果结果为false,则不返回任何值

return bounds;语句移到if语句之外。如果if语句解析为false,则永远不会返回。

如果depth = Vector2。零,你不返回任何东西。因此,不是所有的代码路径都返回一个值。

返回语句嵌套在条件语句中。因此,如果(depth == Vector2.Zero)该方法将不会返回值

这是因为如果您首先IF将返回false,然后作为错误说Not all code paths return a value。您应该考虑添加一个返回值,或者在这种情况下可以使用一个例外

您需要将最终返回值移出if:

private Rectangle HandleCollision(Rectangle bounds, TileCollision collision, Rectangle tileBounds)
{
  if(depth != Vector2.Zero)
  {
  }
  return bounds;
}

你是这样写的:

private Rectangle HandleCollision(Rectangle bounds, TileCollision collision, Rectangle tileBounds)
{
  if(depth != Vector2.Zero)
  {
     return bounds;
  }
}

这意味着,如果depth == Vector2.Zero什么都没有返回,那么你就会得到你所看到的错误。

如果您的depth != Vector2.Zero返回false

if ( depth != Vector2.Zero ) 

条件?

此时你的方法没有返回任何东西。