0
votes

Using a tile based system I need to separate platform block from the rest and then allow them to move up and down at a constant speed.

How would I make the tile that is labeled '=' (Vertical Moving Platform) to move up or down until it hits either a '#' (Wall) or an end point '+' (Vertical Moving Platform Movement Area)?

There is a camera class that follows the player, a player class that controls the movement and a Block class that controls the collision of blocks.

List were the blocks are stored:

List<Block> Blocks;

List were the levels are stored:

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

Here is where the levels are created(Test Map):

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

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

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

Here is the void LoadLevel that will give meaning the to level(This is called when the player completes the level and in the LoadContent Method):

    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'");
        }
    }

Update Method:

    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);
    }

Draw Method:

    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
Make a class for the object that u would like to move and add collision to it.MCollard
But how do I make it move? How do I call out all the block that is in that position and make it move up or down?Fishir

1 Answers

0
votes
  • Make a class like Block( something like MovingBlock )

  • Add an update method in the class so the block moves up and down.

  • Use the class in your LoadLevel

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