-1

好的,所以在开发我正在开发的 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;
        }
    }

任何帮助理解此错误将不胜感激,谢谢。

4

7 回答 7

3

你的问题就在这里。

if (depth != Vector2.Zero)

如果计算结果为 false,则不会返回任何内容。

于 2013-03-07T20:18:42.117 回答
0

将您的陈述移到return bounds;陈述之外if。如果该if语句解析为 false,则永远不会命中返回。

于 2013-03-07T20:18:51.860 回答
0

如果深度等于 Vector2.Zero,则不会返回任何内容。因此,并非所有代码路径都返回一个值。

于 2013-03-07T20:19:32.453 回答
0

如果你depth != Vector2.Zero回来怎么false

if ( depth != Vector2.Zero ) 

健康)状况?

您的方法此时不会返回任何内容。您还必须在此循环之外返回值。

于 2013-03-07T20:19:50.413 回答
0

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

于 2013-03-07T20:20:14.393 回答
0

这是因为如果你先IF返回false,那么就像错误所说的那样Not all code paths return a value。您应该考虑添加返回值,或者可能是这种情况的例外

于 2013-03-07T20:21:31.610 回答
0

您需要将最终返回值移到 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没有返回任何内容,那么您会收到您所看到的错误。

于 2013-03-07T20:22:43.877 回答