-1

Ok so while working on an XNA 4.0 game I'm developing I came across this problem with one of my methods which is getting the error 'not all code paths return a value' and well this has been driving me insane for the past couple hours.

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

Any help understanding this error would be appreciated, thanks.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Kyle
  • 23
  • 3
  • What does it return if `depth == Vector2.Zero`? – Joachim Isaksson Mar 07 '13 at 20:19
  • You should thing about making bounds an `out` parameter. – Jordan Kaye Mar 07 '13 at 20:19
  • Your return statement is inside an if block - any code which will not enter that if block will not return anything but your method requires to return a Rectangle type. Having the return statement outside the if block should resolve the issue. – Satyajit Mar 07 '13 at 20:20
  • return bounds; is inside your main if statement. is there a default value you can return? either way, you need to return something outside of your if statement, in case it never makes it to the return bounds; line. maybe create bound before the if, set it to null, and return after the if statement. – mmeasor Mar 07 '13 at 20:21
  • Cheers guys, sorry for that I feel really stupid now. – Kyle Mar 07 '13 at 20:52

7 Answers7

3

Your issue is here.

if (depth != Vector2.Zero)

if this evaluates to false, nothing gets returned.

scartag
  • 17,548
  • 3
  • 48
  • 52
0

Move your return bounds; statement outside the if statement. If that if statement resolves to false then the return is never hit.

Justin Chmura
  • 1,939
  • 1
  • 15
  • 17
  • Thanks a lot this worked, cant believe it was that simple I feel like a complete idiot now.. – Kyle Mar 07 '13 at 20:29
0

If depth is equal to Vector2.Zero you don't return anything. Hence, not all code paths return a value.

Roman Royter
  • 1,655
  • 13
  • 28
0

What if your depth != Vector2.Zero return false in

if ( depth != Vector2.Zero ) 

condition?

Your method doesn't return anything at this point. You must return value also outside of this loop.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0

The return statement is nested inside of your conditional. So if (depth == Vector2.Zero) the method will not return a value.

smedge
  • 1
0

it's because if you first IF would return false then as the error says Not all code paths return a value. you should consider adding a return value or may be an exception for that case

Arash Milani
  • 6,149
  • 2
  • 41
  • 47
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.

Mike C.
  • 3,024
  • 2
  • 21
  • 18