An endless climb with only 10 platforms
- PROBLEM
- An endless vertical level can't be built in advance, and creating and destroying platforms every few seconds adds allocation and garbage-collection pressure in the browser.
- SOLUTION
- Ten platforms are created once. When one scrolls past the bottom of the screen it is recycled: moved above the current highest platform with a random vertical gap (150–300px) and horizontal offset (up to ±250px from the centre).
- RESULT
- A fixed object count for the whole run. The level never ends, but nothing new is allocated for it during gameplay.
// scenes/Game.js (simplified): recycle instead of destroy
if (platform.getBounds().bottom > this.game.config.height) {
this.positionPlatform(platform);
}
positionPlatform(platform) {
const top = this.getHighesPlatform();
platform.y = top - this.randomValue(gameOptions.platformVerticalDistanceRange);
platform.x = this.game.config.width / 2
+ this.randomValue(gameOptions.platformHorizontalDistanceRange)
* Phaser.Math.RND.sign();
}