好的,所以在开发我正在开发的 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;
}
}
任何帮助理解此错误将不胜感激,谢谢。