Now we have the physical body of the platform and we can make the platform move by moving its physical body. Then we will move the platform container with all the tiles to the new position of the physical body.
Let’s create a new move method, in which we will set the physical body to a new position, taking into account the platform speed specified in the this.dx property.
export class Platform {
// ...
move() {
if (this.body) {
Matter.Body.setPosition(this.body, {x: this.body.position.x + this.dx, y: this.body.position.y});
this.container.x = this.body.position.x - this.width / 2;
this.container.y = this.body.position.y - this.height / 2;
}
}
}
After setting the physical body to a new position, move the platform container to the same position, thus moving all the tiles of the platform to the correct place.
It remains to run the move method for all created platforms in the Platforms class:
export class Platforms {
// ...
update() {
// ...
this.platforms.forEach(platform => platform.move());
}
}