XNA平台冲突无法正常工作

时间:2014-12-17 21:44:13

标签: c# xna collision

所以我在XNA中制作一个简单的平台游戏,当我将两个平台加在一起时,我会连接它们所在的位置,如果只有一个平台就可以了。我可以通过使每个不同的平台成为自己的精灵来解决这个问题,但这意味着每个平台都有一个精灵。

以下是我如何列出我的平台。

        platformList.Add(new Platform(txGrassplatformmid, new Vector2(0,800)));
        platformList.Add(new Platform(txGrassplatformmid, new Vector2(90, 800)));
        platformList.Add(new Platform(txGrassplatformmid, new Vector2(200, 800)));
        platformList.Add(new Platform(txGrassplatformmid, new Vector2(300, 800)));
        platformList.Add(new Platform(txGrassplatformmid, new Vector2(400, 800)));
        platformList.Add(new Platform(txGrassplatformmid, new Vector2(500, 800)));

代码本来不是我的代码,是老师给我修改我年终项目的,但我认为这是碰撞代码。

        public virtual void Draw(SpriteBatch spriteBatch)
    {

        spriteBatch.Draw(texture, GetTopLeft(), tintColour);

        //texture, position, null, Color.White, angle, Vector2.Zero, spriteEffects, 1);
    }

    public Vector2 GetTopLeft()
    {

        // the value stored is the base midde for platfrom collisions.

        return new Vector2(position.X - (enclosingRectangle.Width / 2), position.Y - enclosingRectangle.Height);
    }

    public Rectangle GetRectangle()
    {
        // get enclosing erctangle, adjusting for the position representing middle of base

        return new Rectangle((int)(position.X - (width / 2)), (int)(position.Y - height), (int)width, (int)height);


    }

------------------------编辑---------------------- ----------

在弄乱了一些代码之后我就

 public bool OnPlatform(Vector2 oldPosition, ref Vector2 newPosition, float playerHalfWidth)
    {

        // where you above the platform before and below or on it now?
        if ((oldPosition.Y <= Y) && (newPosition.Y >= Y))
        {
            // we need to work out 7where you hit the platform
            // for simplicity we use newPositionX - would be better to
            // calculate an X position between OldPosition.X and newPosition.X

            if (((newPosition.X + playerHalfWidth) >= X) && ((newPosition.X - playerHalfWidth) <= (X + width)))
            {
                newPosition.Y = Y;
                return true;
            }
        }
        return false;
    }

如果我改变了返回true;返回虚假;除了精灵之外,碰撞工作得很好,不能再跳,他就像滑冰鞋一样滑动而不是停下来。

0 个答案:

没有答案
相关问题