-1

わかりましたので、私が開発している XNA 4.0 ゲームで作業しているときに、「すべてのコード パスが値を返すわけではありません」というエラーが発生するメソッドの 1 つでこの問題に遭遇しました。 .

 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 ステートメントは、条件内にネストされています。したがって、(depth == Vector2.Zero) の場合、メソッドは値を返しません。

于 2013-03-07T20:20:14.393 に答える
0

return bounds;ステートメントをステートメントの外に移動しますif。そのifステートメントが false に解決された場合、リターンはヒットしません。

于 2013-03-07T20:18:51.860 に答える
0

depth が Vector2.Zero と等しい場合、何も返されません。したがって、すべてのコード パスが値を返すわけではありません。

于 2013-03-07T20:19:32.453 に答える
0

あなたがdepth != Vector2.Zero戻っfalseてきたらどうしますか

if ( depth != Vector2.Zero ) 

調子?

この時点では、メソッドは何も返しません。このループの外でも値を返す必要があります。

于 2013-03-07T20:19:50.413 に答える
0

それは、エラーが示すように、最初IFに戻った場合です。戻り値を追加することを検討するか、その場合の例外になる可能性がありますfalseNot all code paths return a value

于 2013-03-07T20:21:31.610 に答える
0

You need to move your final return value outside of your if:

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

  }
  return bounds;
}

you have it like this:

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

What this means, is if depth == Vector2.Zero nothing gets returned, so you get the error that you are seeing.

于 2013-03-07T20:22:43.877 に答える