Dynamically creating buttons but won't retrieve data from it

You could make a variable outside of the for-loop that is a reference to the scene:

let myScene;
    create() {
        myScene = this;
        for (var a=1; a<=10; a++) {
            var mybutton = this.add.image(60*a, 30, "btnLevel").setInteractive();
            mybutton.myid = "btnLevel" + a;
            mybutton.on("pointerup", function (e) {
               myScene.scene.switch("sceneNext", { 
                   "level": this.myid}); 
            });
        }
    }
1 Like