2

我已经设法获得了我认为他们需要获得的值,以便 snape 和平台边界框之间的碰撞检测发生在平台类中。但是它不起作用。没有错误,我看不出哪里出错了。您的帮助将不胜感激。我的3节课如下。

游戏1类

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

    Texture2D background;
    Movement character;
    Platform[] platforms;
    //private Vector2 SnapePosition = Vector2.Zero;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        graphics.PreferredBackBufferHeight = 440;
        graphics.PreferredBackBufferWidth = 782;

    }


    protected override void Initialize()
    {
        // TODO: Add your initialization logic here
        platforms = new Platform[15];


        base.Initialize();
    }


    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        character = new Movement(Content.Load<Texture2D>("snape"), new Rectangle(0, 350, 50, 50));
        for (int i = 0; i < platforms.Length; i++)
        {
            platforms[i] = new Platform(
                Content.Load<Texture2D>("Platforms/lvl2_platform"), new Rectangle(i*100, 410, 100, 30), character.Snape, character.SnapePosition);
        }

        // TODO: use this.Content to load your game content here
        background = Content.Load<Texture2D>("Backgrounds/lvl2_background");
    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        //Allows the player to move
        character.Update();

        // TODO: Add your update logic here


        base.Update(gameTime);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        // TODO: Add your drawing code here

        spriteBatch.Begin();
        spriteBatch.Draw(background, Vector2.Zero, Color.White);
        character.Draw(spriteBatch);

        foreach (Platform platform in platforms)
        {
            platform.Draw(spriteBatch);
        }
        spriteBatch.End();

        base.Draw(gameTime);
    }
}
}

玩家等级

class Player
{
    public Texture2D Snape;
    public Rectangle SnapePosition;


    public virtual void Update()
    {

    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(Snape,SnapePosition,Color.White);
    }
}

class Movement : Player
{
        public Movement(Texture2D newSnape, Rectangle newSnapePosition)
    {
        Snape = newSnape;
        SnapePosition = newSnapePosition;
    }



        public override void Update()
        {
            KeyboardState keyBoard = Keyboard.GetState();

            if (keyBoard.IsKeyDown(Keys.A))
            {
                SnapePosition.X -= 5;
            }
            if (keyBoard.IsKeyDown(Keys.D))
            {
                SnapePosition.X += 5;
            }
            if (keyBoard.IsKeyDown(Keys.W))
            {
                SnapePosition.Y -= 5;
            }
            if (keyBoard.IsKeyDown(Keys.S))
            {
                SnapePosition.Y += 5;
            }

        }
}
}

平台类

class Platform
{
    Texture2D texture;
    Rectangle rectangle;
    Texture2D snape;
    Rectangle snapePosition;
    public Rectangle test;


    public enum CollisionPosition { None, Top, Bottom, Left, Right };
    public CollisionPosition collisionType;
    public bool inCollision;
    public int collisionDepth;

    public Platform(Texture2D newTexture, Rectangle newRectangle, Texture2D newSnape, Rectangle newSnapePos)
    { 
        texture = newTexture;
        rectangle = newRectangle;
        snapePosition = newSnapePos;
        snape = newSnape;
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(texture, rectangle, Color.White);
    }

    public void Collisions()
    {
        if (rectangle.Intersects(snapePosition))
        inCollision = true;
    }

    public void DetermineCollisionType()
    {
        if (inCollision == false)
        {
            collisionType = CollisionPosition.None;
            collisionDepth = 0;
        }
        else
        {
            // Determine the side of *least intersection* for snape
            int minOverlap = int.MaxValue;

            // Check the top side
            int tOverlap =
                (rectangle.Y + texture.Height / 2)
                - (snapePosition.Y - snape.Height / 2);
            if (tOverlap > 0 && tOverlap < minOverlap)
            {
                collisionType = CollisionPosition.Top;
                minOverlap = tOverlap;
            }

            // Check the bottom side
            int bOverlap =
                (snapePosition.Y + snape.Height / 2)
                - (rectangle.Y - texture.Height / 2);
            if (bOverlap > 0 && bOverlap < minOverlap)
            {
                collisionType = CollisionPosition.Bottom;
                minOverlap = bOverlap;
            }

            // Check the right overlap
            int rOverlap =
                (snapePosition.X + snape.Width / 2)
                - (rectangle.X - texture.Width / 2);
            if (rOverlap > 0 && rOverlap < minOverlap)
            {
                collisionType = CollisionPosition.Right;
                minOverlap = rOverlap;
            }

            // Check the left overlap
            int lOverlap =
                (rectangle.X + texture.Width / 2)
                - (snapePosition.X - snape.Width / 2);
            if (lOverlap > 0 && lOverlap < minOverlap)
            {
                collisionType = CollisionPosition.Left;
                minOverlap = lOverlap;
            }

            // Update the collision depth
            collisionDepth = minOverlap;
        }
    }

    public void SeparateSnape()
    {
        switch (collisionType)
        {
            case CollisionPosition.None:
                break;
            case CollisionPosition.Top:
                snapePosition.Y += collisionDepth;
                break;
            case CollisionPosition.Bottom:
                snapePosition.Y -= collisionDepth;
                break;
            case CollisionPosition.Right:
                snapePosition.X -= collisionDepth;
                break;
            case CollisionPosition.Left:
                snapePosition.X += collisionDepth;
                break;
        }
    }

    public void Update()
    {
        // Check for collision
        Collisions();

        // Determine collision type
        DetermineCollisionType();

        // Separate snape
        SeparateSnape();
    }

    public Rectangle getSnapePos
    {
        get { return snapePosition; }
    }
}

}
4

2 回答 2

1

您的部分问题似乎是您不知道类和结构之间的区别是什么。矩形是一个结构,这意味着当您将它作为参数传递给任何方法时,它会“按值”传递,这意味着该值的副本已生成,并且只要方法正在运行,它就会一直存在,之后它不再存在。另一方面,类是引用类型,因此如果您将类作为参数传递,您将“通过引用”传递它,这意味着该方法将获得一个值,告诉它在哪里访问该类。这里的区别在于,虽然引用在方法结束时被销毁,但它只是一个引用,而不是值本身,这与结构不同。

最重要的是,即使你给平台一个对 snape 位置的引用而不是一个值,当你移动 snape 时,你会更改他位置的整个值,而不仅仅是它的一部分,所以它会覆盖 snape 的引用,无论如何都会使平台的参考资料过时。

老实说,我认为您最好重新考虑一下您的设计。首先,尝试将键盘管理移到角色之外,因此游戏本质上会挂接到一个角色并控制该角色的移动。这很重要,因为这意味着游戏本身知道主角是谁,角色可以与哪些平台发生碰撞,从而可以对平台和角色进行碰撞检查。如果平台不能改变角色的位置并且玩家不知道地图上的所有平台是什么,那么让游戏本身监控似乎更明智。

这是一个例子:

protected override void Update()
        {
            KeyboardState keyboardState = Keyboard.GetState();
            Rectangle oldBounds = player.Bounds;
            if (keyboardState.IsKeyDown(Keys.W)) { player.Y--; }
            if (keyboardState.IsKeyDown(Keys.A)) { player.X--; }
            if (keyboardState.IsKeyDown(Keys.X)) { player.Y++; }
            if (keyboardState.IsKeyDown(Keys.D)) { player.X++; }
            foreach (Platform platform in platforms)
            {
                if (player.Bounds.Intersects(platform.Bounds))
                {
                    player.Bounds = oldBounds;
                    break;
                }
            }
        }

你会想让它更复杂一点,但这可能是最基本的碰撞系统。显然,如果你在做一个平台游戏,你可能会想要更聪明一点。我建议您尽可能多地进行研究和实验,还请务必查看如何在代码中添加断点并逐行运行代码,以及使用本地和监视框来弄清楚到底发生了什么当您的代码运行时。如果您可以确定正在发生的事情和应该发生的事情,它将使事情更容易解决和理解。

于 2013-06-14T05:31:17.600 回答
1
protected override void Update(GameTime gameTime)
{
    // Allows the game to exit
    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
        this.Exit();

    //Allows the player to move
    character.Update();

    // TODO: Add your update logic here


    base.Update(gameTime);
}

我没有看到有关您的平台的任何更新。

尝试添加

 foreach (Platform platform in platforms)
    {
        platform.Update();
    }
于 2012-10-20T15:36:16.447 回答