0

我正在努力做到这一点,以便我的球员在击球时完全停止,但如果他按下某个键,他可能会变得不卡住。示例:玩家正在下降并撞到了一个障碍物。他停下来,但如果按 W、A 或 D,他可以向上、向左或向右移动。这是我目前拥有的代码。

在这种情况下,sp 表示速度,blocksp 表示他被阻挡时的速度。(This.Sprite),在这种情况下,指的是阻止他的块。

        bool Blocked = false;
        float Bottom = -32;
        float Left = -32;
        float Right = This.Sprite.GetWidth() - 32;
        float Top = This.Sprite.GetHeight() - 32;
        int x = 0;
        int y = 0;
        float sp = 1.59f;
        float blocksp = 0.00f;
        Sprite Player = This.Game.FindSprite("GuySprite");
        if (This.Sprite.CollisionWithSprite("GuySprite") != null)
        {

            if (Player.Position.Y > Top)
            {
                Blocked = true;
                Player.Velocity = new Point2D(x, y) * blocksp;
                Player.Animation = 0;
                if (This.Game.IsPressed(InputKey.W) || This.Game.IsPressed(InputKey.A) || This.Game.IsPressed(InputKey.D))
                {
                    Blocked = false;
                    Player.Velocity = new Point2D(x, y) * sp;
                    Player.Position.Y -= 0.85f;
                }
            }
            if (Player.Position.Y < Bottom)
            {
                Blocked = true;
                Player.Velocity = new Point2D(x, y) * blocksp;
                Player.Animation = 0;
                if (This.Game.IsPressed(InputKey.S) || This.Game.IsPressed(InputKey.A) || This.Game.IsPressed(InputKey.D))
                {
                    Blocked = false;
                    Player.Velocity = new Point2D(x, y) * sp;
                    Player.Position.Y += 0.85f;
                }
            }
            if (Player.Position.Y < Right)
            {
                Blocked = true;
                Player.Velocity = new Point2D(x, y) * blocksp;
                Player.Animation = 0;
                if (This.Game.IsPressed(InputKey.S) || This.Game.IsPressed(InputKey.A) || This.Game.IsPressed(InputKey.W))
                {
                    Blocked = false;
                    Player.Velocity = new Point2D(x, y) * sp;
                    Player.Position.Y -= 0.85f;
                }
            }
            if (Player.Position.Y > Left)
            {
                Blocked = true;
                Player.Velocity = new Point2D(x, y) * blocksp;
                Player.Animation = 0;
                if (This.Game.IsPressed(InputKey.S) || This.Game.IsPressed(InputKey.W) || This.Game.IsPressed(InputKey.D))
                {
                    Blocked = false;
                    Player.Velocity = new Point2D(x, y) * sp;
                    Player.Position.X += 0.85f;
                }
            }
        }

目前,如果我的玩家接触到方块的任何一侧并继续移动到方块停止,那么玩家将朝一个不稳定的方向移动并且不会在方块处停止。有任何想法吗?

4

1 回答 1

1

编辑

好的这是图片。

玩家正在向街区奔跑,因此他将在接下来的帧中击中左侧。这样下面的条件就会成立。

if (This.Sprite.CollisionWithSprite("GuySprite") != null)

现在您必须实现一种方法来找出玩家与哪一侧发生碰撞。实际上,您的代码使我感到困惑,因为:

Player.Velocity = new Point2D(x, y) * ...; 

将始终是零向量,因为 x = y = 0

我会这样做:

float xDistance=0f, yDistance=0f;//this ll be explained later on
If(Player.Velocity.X > 0) //he is running to the right so he can hit the left side
     xDistance=mayHitLeft();
Else If(Player.Velocity.X < 0)
     xDistance=mayHitRight();
If(Player.Velocity.Y > 0) //In my case positive Y means downwards so can hit top
     yDistance=mayHitTop();
Else If(Player.Velocity.Y < 0)
     yDistance=mayHitBot();

现在我们必须确定玩家是否击中了机器人/上边缘或左/右边缘,或者两者兼而有之。让我们假设他正在摔倒并且之前跑到右边。soPlayer.Velocity.X > 0Player.Velocity.Y > 0 这意味着函数mayHitLeft();mayHitTop(); 将被调用。我不知道这个collisionWithSprite功能是如何工作的。但与往常一样,我们会采取最坏的情况,比如蓝色是方块,橙色是玩家。这是击中之前的帧,现在没有检测到碰撞。所以在下一帧中,两个函数都会检测到碰撞,但这是正确的。正如我们所见,它正在与顶部相撞。那么如何检测正确的呢?因此,我们使用距离,或者说我们在哪个方向上有多少重叠。所以较少的重叠是正确的。

所以这两个函数都返回一个双精度值。我们来看看mayHitTop()函数

private float mayhitTop()
    {
        //remember that the y coordinate goes downwards so we have to add
        float playersBotCoordinate = Player.Position.Y + Player.Sprite.Height;
        //the y position is the top so nothing to change
        float blocksTopCoordinate = Block.Position.Y;
        hitTop=true; //this is a global variable you have 1 for each direction
        return Math.Abs(playersBotCoordinate - blocksTopCoordinate);
    }

所以我们设置了xDistanceandyDistance我们知道它会到达顶部或左侧。

现在我们必须比较一切

// this means there could be 2 sides like our case that can be ths possible collision edge
if((hitLeft || hitRight) && (hitTop || hitBot))
{ 
   //we hit the left or right side
   if(xDistance<yDisante)
   {
      Player.Velocity.X = 0;
   }
   //we hit top or bottom
   else
   {         
      Player.Velocity.Y=0;
   }
}
else
{
    if(hitLeft || hitRight)...

}

也许你必须解开播放器。否则我认为可能会有一些问题。

所以我只是在没有任何模板的情况下写了它。不知道有没有语法错误。但这只会给你一个概述如何做到这一点'我们可以预期这是伪代码;)

希望它至少对你有一点帮助。

老的

嗯,您正在比较玩家位置。Y 与左右,如果我理解代码,这应该是 X 坐标;)

 if (Player.Position.Y < Right) 

if (Player.Position.Y > Left)  
于 2012-09-04T05:46:12.513 回答