在列表中移动对象,与播放器发生冲突

时间:2014-04-24 13:33:51

标签: c# list xna collision

我最近问了一个问题,他们应该如何与瓷砖地图发生碰撞,并且由于时间的限制,我在游戏中无法做出答案,我即兴创作并让它发挥作用。但现在我正在转向另一个在编码时出现的问题,即我的平台突然停止与玩家发生碰撞。一秒钟他们就可以了,他们没有。

我看了一下,看到平台与玩家互动的方式没有区别,但有些看法不起作用。我不知道他们将如何发生。

Here 是关于他们如何与瓷砖碰撞的最后一篇文章。

Here 是项目的链接,如果您只想查看这堆垃圾代码。

我使用三个类来移动平台,主类Game1,MovingPlatform和Block。 Game1运行所有这些,MovingPlatform帮助列表并使平台打勾,Block帮助与瓷砖碰撞。

问题

我做错了什么让平台不碰撞?

我将从MovingPlatform类开始:

public class MovingPlatform
{
    Game1 game1;

    public Texture2D Texture;
    public Vector2 Velocity;
    public Vector2 Position;
    public Rectangle Size;
    public Rectangle World;
    public float Speed;
    public bool Up;
    public bool Thing;
    float Time;

    public MovingPlatform(Texture2D Texture, Vector2 Position,float Speed, bool Up)
    {
        this.Texture = Texture;
        this.Position = Position;
        this.Speed = Speed;
        this.Up = Up;
        Thing = true;
        Velocity = Vector2.Zero;
        Size = new Rectangle((int)Position.X, (int)Position.Y, 50, 25);
    }

    public void Initialize(Game1 game1)
    {
        this.game1 = game1;

    }

    public Player BlockCollision(Player player)
    {
        Rectangle top = new Rectangle((int)Position.X + 5, (int)Position.Y - 10, Size.Width - 10, 10);
        Rectangle bottom = new Rectangle((int)Position.X + 5, (int)Position.Y + Size.Height, Size.Width - 10, 10);
        Rectangle left = new Rectangle((int)Position.X - 10, (int)Position.Y + 5, 10, Size.Height - 10);
        Rectangle right = new Rectangle((int)Position.X + Size.Width, (int)Position.Y + 5, 10, Size.Height - 10);

        if (!player.goingUp)
        {
            if (top.Intersects(new Rectangle((int)player.Position.X, (int)player.Position.Y, player.Texture.Width, player.Texture.Height)))
            {
                if (player.Position.Y + player.Texture.Height > Position.Y && player.Position.Y + player.Texture.Height < Position.Y + Texture.Height / 2)
                {
                    player.Position.Y = player.ground = Position.Y - player.Texture.Height;
                    player.Velocity.Y = 0;
                    player.isJumping = false;
                    player.Time = 0;
                    player.botCollision = true;
                }

            }
            else if (player.isJumping == false && player.ground == Position.Y - player.Texture.Height)
            {
                player.isJumping = true;
                player.initialVelocity = 0;
            }

        }
        return player;
    }

    public void Update(GameTime gameTime)
    {
        Time += (float)gameTime.ElapsedGameTime.TotalSeconds;
        World = new Rectangle(0, 0, 0, 0);

        if (Up == true)
        {
            if (Thing == true)
            {
                Position.Y -= 2;
            }
            else if (Thing == false)
            {
                Position.Y += 2;
            }
        }

        if (Up == false)
        {
            if (Thing == true)
            {
                Position.X -= 2;
            }
            else if (Thing == false)
            {
                Position.X += 2;
            }
        }
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(Texture, new Rectangle((int)Position.X, (int)Position.Y, Size.Width, Size.Height), Color.White);
    }
}
}

块:

public class Block
{
    Game1 game1;

    public Texture2D Texture;
    public Vector2 Position;
    public int BlockState;
    KeyboardState prevKB;
    KeyboardState kbState;
    public static bool NextLevel = false;
    Rectangle Contain;


    public Block(Texture2D Texture, Vector2 Position, int BlockState)
    {
        this.Texture = Texture;
        this.Position = Position;
        this.BlockState = BlockState;

        Contain = new Rectangle((int)Position.X, (int)Position.Y, 50, 50);
    }

    public void Initialize(Game1 game1)
    {
        this.game1 = new Game1();
    }

    public Player BlockCollision(Player player, GameTime gameTime, Game1 game1)
    {
        this.game1 = game1;
        kbState = Keyboard.GetState();

        Rectangle BlockRectangle = new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height);

        Rectangle top = new Rectangle((int)Position.X + 5, (int)Position.Y - 10, Texture.Width - 10, 10);
        Rectangle bottom = new Rectangle((int)Position.X + 5, (int)Position.Y + Texture.Height, Texture.Width - 10, 10);
        Rectangle left = new Rectangle((int)Position.X - 10, (int)Position.Y + 5, 10, Texture.Height - 10);
        Rectangle right = new Rectangle((int)Position.X + Texture.Width, (int)Position.Y + 5, 10, Texture.Height - 10);

        if (BlockState > 0)
        {
            for (int i = 0; i < game1.Platforms.Count; i++)
            {
                if (top.Intersects(new Rectangle((int)game1.Platforms[i].Position.X, (int)game1.Platforms[i].Position.Y, game1.Platforms[i].Texture.Width, game1.Platforms[i].Texture.Height)))
                {
                    game1.Platforms[i].Thing = true;
                }

                if (bottom.Intersects(new Rectangle((int)game1.Platforms[i].Position.X, (int)game1.Platforms[i].Position.Y, game1.Platforms[i].Texture.Width, game1.Platforms[i].Texture.Height)))
                {
                    game1.Platforms[i].Thing = false;
                }

                if (left.Intersects(new Rectangle((int)game1.Platforms[i].Position.X, (int)game1.Platforms[i].Position.Y, game1.Platforms[i].Texture.Width, game1.Platforms[i].Texture.Height)))
                {
                    game1.Platforms[i].Thing = false;
                }

                if (right.Intersects(new Rectangle((int)game1.Platforms[i].Position.X, (int)game1.Platforms[i].Position.Y, game1.Platforms[i].Texture.Width, game1.Platforms[i].Texture.Height)))
                {
                    game1.Platforms[i].Thing = false;
                }
            }
        }
        prevKB = Keyboard.GetState();

        return player;
    }

然后最后是Game1班:

    public List<Block> Blocks;
    public List<MovingPlatform> Platforms;

    protected override void Initialize()
    {

        Blocks = new List<Block>();
        Platforms = new List<MovingPlatform>();

        base.Initialize();
    }

    protected override void LoadContent()
    {
        movingPlatform = new MovingPlatform(ballSprite, Vector2.Zero, 3.0f, true);
        movingPlatform.Initialize(this);
    }

    void LoadLevel(int level)
    {
        Blocks.Clear();
        Platforms.Clear();

        Texture2D platform = Content.Load<Texture2D>("Platform");

        for (int x = 0; x < tileWidth; x++)
        {
            for (int y = 0; y < tileHeight; y++)
            {
                //Platform Stoper
                if (Levels[level][y, x] == '+')
                {
                    Blocks.Add(new Block(movingArea, new Vector2(x * 50, y * 50), 3));
                }
                //Vertical Moving Platform
                if (Levels[level][y, x] == '=')
                {
                    Platforms.Add(new MovingPlatform(platform, new Vector2(x * 50, y * 50), 3.0f, true));
                }
            }
        }
    }

    protected override void Update(GameTime gameTime)
    {
        foreach (Block b in Blocks)
        {
            player = b.BlockCollision(player, gameTime, this);
        }

        foreach (MovingPlatform m in Platforms)
        {
            player = m.BlockCollision(player);
            m.Update(gameTime);
        }
    }

    protected override void Draw(GameTime gameTime)
    {
        spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullCounterClockwise, null, camera.Transform());
        foreach (Block b in Blocks)
        {
            b.Draw(spriteBatch);
        }

        foreach (MovingPlatform m in Platforms)
        {
            m.Draw(spriteBatch);
        }

        spriteBatch.End();

        base.Draw(gameTime);
    }
}

0 个答案:

没有答案