Platforms generating in Phaser

Hi, I’m a beginner to Phaser and would like to generate some platforms for a platformer game. I want to reuse them, as the camera follows the player. However, my platforms aren’t generating as I would like them to. Does anyone know what I’m doing wrong, or how I can randomly generate platforms that are within the player’s jumping distance, horizontally?

Thanks a lot. :slight_smile:

Here is code:

    create() {
        // Create platforms
        this.platforms = this.physics.add.staticGroup();
        this.lastPlatformX = 0; // x-coordinate of last platform
        this.lastPlatformY = 0; // y-coordinate of last platform

        this.firstPlatformX = 0; // x-coordinate of first platform
        this.firstPlatformY = 0; // y-coordinate of first platform

        // Create first platform
        let x = 100;
        let y = 300;
        let platform = this.platforms.create(x, y, "platform");
        platform.scale = 0.5;
        let body = platform.body;
        body.updateFromGameObject();

        this.firstPlatformX = x; // x-coordinate of first platform
        this.firstPlatformY = y; // y-coordinate of first platform

        // Create second platform
        x = 400;
        y = 400;
        platform = this.platforms.create(x, y, "platform");
        platform.scale = 0.5;
        body = platform.body;
        body.updateFromGameObject();

        // Create third platform
        x = 400;
        y = 700;
        platform = this.platforms.create(x, y, "platform");
        platform.scale = 0.5;
        body = platform.body;
        body.updateFromGameObject();
        console.log("hej")

        // Create fourth platform
        x = 500;
        y = 900;
        platform = this.platforms.create(x, y, "platform");
        platform.scale = 0.5;
        body = platform.body;
        body.updateFromGameObject();

        // Create fifth platform
        x = 500;
        y = 300;
        platform = this.platforms.create(x, y, "platform");
        platform.scale = 0.5;
        body = platform.body;
        body.updateFromGameObject();

        this.lastPlatformX = x; // save x-coordinate of last platform
        this.lastPlatformY = y; // save y-coordinate of last platform


        // create player above last platform
        this.player = this.physics.add.sprite(this.firstPlatformX, this.firstPlatformY - 50, "dude");
        this.player.setBounce(0.2);
        this.physics.add.collider(this.player, this.platforms);
    }

    update() {

        // reuse platforms
        this.platforms.children.iterate(child => {

            const platform = child
            const scrollY = this.cameras.main.scrollY
            if (platform.y >= scrollY + 600) {
                platform.y = scrollY - Phaser.Math.Between(50, 100)
                platform.body.updateFromGameObject()
            }
        })

     
    
        });

        if (this.player.y > this.lastPlatformY + 200) {
            this.scene.start('gameOver')
        }

    }
}

:wave:

if (platform.y >= scrollY + 600) {
  const x = player.x + (Phaser.Math.FloatBetween(0, 1) < 0.5 ? -100 : 100);
  const y = scrollY - Phaser.Math.Between(50, 100);

  platform.body.reset(x, y);
}