带有移动平台的瓷砖系统

时间:2014-03-25 13:11:52

标签: c# list xna 2d tile

使用基于图块的系统我需要将平台块与其余部分分开,然后允许它们以恒定速度上下移动。

如何制作贴有标签的瓷砖' =' (垂直移动平台)向上或向下移动,直到它碰到一个'#' (墙)或终点' +' (垂直移动平台移动区域)?

在玩家后面有一个相机类,一个控制移动的玩家类和一个控制块碰撞的Block类。

列表是存储的块:

List<Block> Blocks;

列表是存储的级别:

List<char[,]> Levels = new List<char[,]>();

这是创建关卡的位置(测试地图):

    protected override void Initialize()
    {
        Blocks = new List<Block>();

        char[,] Level2 = {{'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
                          {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','+','.'},
                          {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
                          {'.','.','.','.','.','.','.','.','.','.','.','.','#','.','.','.'},
                          {'.','.','.','.','.','.','.','-','-','-','.','.','#','.','.','.'},
                          {'.','.','.','.','.','.','.','.','.','.','.','.','#','.','.','.'},
                          {'.','.','.','-','-','-','.','.','.','.','.','.','#','.','.','.'},
                          {'.','.','.','.','.','.','.','.','.','.','.','.','#','.','.','.'},
                          {'#','#','.','.','.','.','.','.','.','P','.','.','#','.','=','.'},
                          {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'}};

        Levels.Add(Level2);
        base.Initialize();
    }

这是void LoadLevel,它将赋予level的含义(当玩家完成关卡和LoadContent方法时调用它):

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

        player.Position = Vector2.Zero;

        tileWidth = Levels[level].GetLength(1);
        tileHeight = Levels[level].GetLength(0);
        for (int x = 0; x < tileWidth; x++)
        {
            for (int y = 0; y < tileHeight; y++)
            {
                //Background
                Blocks.Add(new Block(background, new Vector2(x * 50, y * 50), 0));

                //Impassable Blocks
                if (Levels[level][y, x] == '#')
                {
                    Blocks.Add(new Block(blockSpriteA, new Vector2(x * 50, y * 50), 1));
                }
                //Blocks that are only passable if going up them
                if (Levels[level][y, x] == '-')
                {
                    Blocks.Add(new Block(blockSpriteB, new Vector2(x * 50, y * 50), 2));
                }
                //Vertical Moving Platform Movement Area
                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] == '=')
                {
                    Blocks.Add(new Block(movingPlatform, new Vector2((x * 50), (y * 50) + movingPlatform.Height), 4));
                }
                //PlayerSpawn
                if (Levels[level][y, x] == 'P' && player.Position == Vector2.Zero)
                {
                    player.Position = new Vector2(x * 50, (y + 1) * 50 - player.Texture.Height);
                    player.Velocity = new Vector2(0, 0);
                    player.initialVelocity = 0;
                    player.Time = 0;
                    player.isJumping = false;  
                }
                else if (Levels[level][y, x] == 'P' && player.Position != Vector2.Zero)
                {
                    throw new Exception("Only one 'P' is needed for each level");
                }
            }
        }
        if (player.Position == Vector2.Zero)
        {
            throw new Exception("Player Position needs to be set with 'P'");
        }
    }

更新方法:

    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        // TODO: Add your update logic here
        HandleInput(Keyboard.GetState());
        player.Update(gameTime);

        Time += (float)gameTime.ElapsedGameTime.TotalSeconds;

        foreach (Block b in Blocks)
        {
            player = b.BlockCollision(player);
        }
        camera.thing(player);

        prevKB = Keyboard.GetState();

        base.Update(gameTime);
    }

绘制方法:

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

        // TODO: Add your drawing code here
        spriteBatch.Begin(SpriteSortMode.Texture, BlendState.AlphaBlend, null, null, null, null, camera.Transform());
        //spriteBatch.Begin();

        foreach (Block b in Blocks)
        {
            b.Draw(spriteBatch);
        }
        spriteBatch.End();
        base.Draw(gameTime);
    }

1 个答案:

答案 0 :(得分:0)

  • 创建类似Block的类(类似于MovingBlock

  • 在类中添加更新方法,以便块上下移动。

  • 在LoadLevel中使用该类

    if (Levels[level][y, x] == '=')
    { MovingBlocks.Add(new MovingBlock(movingPlatform, new Vector2((x * 50), (y * 50) + movingPlatform.Height) , 4)); }