0

好的,所以我正在制作一个 2d 游戏,我也是新的病态复制/粘贴我的整个 Game.cs 脚本除了 Program.cs 之外,我的项目中没有其他分类

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;



    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }


    protected override void Initialize()
    {

        base.Initialize();
    }


    bool hasJumped = true;


    Vector2 velocity;
    Texture2D player;
    Texture2D ground1;



    Vector2 playerPosition = new Vector2(30, 30);
    Vector2 ground1p1 = new Vector2(0,430);
    Vector2 ground1p2 = new Vector2(200,430);
    Vector2 ground1p3 = new Vector2(0, 310);
    Vector2 ground1p4 = new Vector2(200, 310);


    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);

        player = Content.Load<Texture2D>("Player");
        ground1 = Content.Load<Texture2D>("Ground1");

    }


    protected override void UnloadContent()
    {

    }


    protected override void Update(GameTime gameTime)
    {
        KeyboardState newState = Keyboard.GetState();







        if ((playerPosition.Y > 0) && (playerPosition.Y < graphics.GraphicsDevice.Viewport.Height-player.Height))
        {

            playerPosition += velocity;

        }


        if (Keyboard.GetState().IsKeyDown(Keys.Right)&&playerPosition.X <=(graphics.GraphicsDevice.Viewport.Width-player.Width))
        {

                velocity.X = 3f;

        }





        else if (Keyboard.GetState().IsKeyDown(Keys.Left) && (playerPosition.X >= 0))
        {

                velocity.X = -3f;

        }
        else velocity.X = 0f;
        if (Keyboard.GetState().IsKeyDown(Keys.Up)&& hasJumped==false)
        {
            playerPosition.Y -= 10f;
            velocity.Y = -4f;
            hasJumped = true;
        }
        if (hasJumped==true)
        {
            velocity.Y += 0.10f;
        }
        if (DetectPlayerAndGround1Collision2(playerPosition,ground1p1,player,ground1) == true)
        {
            hasJumped = false;
            velocity.Y = 0f;
        }
        if (DetectPlayerAndGround1Collision2(playerPosition, ground1p2, player, ground1) == true)
        {
            hasJumped = false;
            velocity.Y = 0f;
        }



        if (hasJumped == false)
        {
            velocity.Y = 0f;
        }


        base.Update(gameTime);
    }


    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);


        spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
        spriteBatch.Draw(ground1,ground1p1 , Color.White);
        spriteBatch.Draw(ground1, ground1p2, Color.White);

        spriteBatch.Draw(ground1, ground1p4, Color.White);


        spriteBatch.Draw(player, playerPosition, Color.White);
        spriteBatch.End();
        base.Draw(gameTime);
    }

 public Boolean DetectPlayerAndGround1Collision2(Vector2 playerPositionM,Vector2 groundPositionM,Texture2D playerM,Texture2D groundM)
    {

        if (playerPositionM.Y <= groundPositionM.Y + groundM.Height && playerPositionM.Y > groundPositionM.Y)
        {   
            if (playerPositionM.X < groundPositionM.X && (playerPositionM.X + playerM.Width < groundPositionM.X + groundM.Width && playerPositionM.X + playerM.Width > groundPositionM.X)) { return true; }
            else if (playerPositionM.X + playerM.Width > groundPositionM.X + groundM.Width && (playerPositionM.X < groundPositionM.X + groundM.Width && playerPositionM.X > groundPositionM.X)) { return true; }
            else if ((playerPositionM.X > groundPositionM.X && playerPositionM.X < groundPositionM.X + groundM.Width) && (playerPositionM.X + playerM.Width < groundPositionM.X + groundM.Width && playerPositionM.X + playerM.Width > groundPositionM.X)) { return true; }
        }
    }






}

有人能告诉我为什么当我在它的边缘时我从我的“ground1”精灵中掉下来吗?我完全不知道我的剧本有多么错误我最后的希望是问一个比我更熟练的人,或者我可能是盲人,我看不到什么愚蠢的谢谢

4

1 回答 1

1

当您尝试检测正方形 A 与正方形 B 的碰撞时,您需要注意很多点。

以一个简单的物理系统为例,其中盒子 A 可以上升并且必须下降,并且我们正在尝试检查盒子 A 的底部与盒子 B 的碰撞,那么您需要先检查 A 和 B 的两个点您可以确定碰撞。

我将把这两个点定义为 AO(A 原点左下角)和 AE(A 扩展右下角)。“地面”也存在相同的两个点。

在运动中,除非盒子 A 的移动速度非常慢,否则你可能会在坠落时垂直落入盒子 B 的边界内。所以首先你需要看看盒子A的底部是接触还是穿过盒子B的顶部:

A.O.Y <= B.E.Y && A.O.Y > B.O.Y

现在,如果返回为假,那么我们仍在下降,如果返回为真,我们要么在 B 的顶部,要么在 B 的内部。

现在你知道你在正确的高度或在平台内,你可以开始检查 A 是否在 B 的顶部:

挂在左边缘,或中间某处,但不在右侧:

A.O.X < B.O.X && (A.E.X < B.E.X && A.E.X > B.O.X)

挂在右侧边缘,或中间某处,但不在左侧:

A.E.X > B.E.X && (A.O.X < B.E.X && A.O.X > B.O.X)

中间某处:

(A.O.X > B.O.X && A.O.X < B.E.X) && (A.E.X < B.E.X && A.E.X > B.O.X)

如果这些条件之一为真,则其他两个为假,因此将它们放在一起检查所有三个:

    EDIT:

flag = false

        If A.O.Y <= B.E.Y && A.O.Y > B.O.Y THEN
            If A.O.X < B.O.X && (A.E.X < B.E.X && A.E.X > B.O.X) THEN
                set flag to true
            ELSE IF A.E.X > B.E.X && (A.O.X < B.E.X && A.O.X > B.O.X) THEN
                set flag to true
            ELSE IF (A.O.X > B.O.X && A.O.X < B.E.X) && (A.E.X < B.E.X && A.E.X > B.O.X) THEN
                set flag to true

return flag

因此,阅读以上内容,我们首先检查您是否垂直在平台上。然后我们按顺序检查,挂在左边缘?悬在右边缘?在界限内?如果这三个中的任何一个为真,我们将其发回,否则我们返回假。

我希望这会有所帮助。

于 2012-11-27T21:29:17.917 回答