erich Posted December 18, 2016 Share Posted December 18, 2016 Hi Im trying to make a level select buttons so a person can choose which level to play How can I make this.levelButton1,this.levelButton2 etc on the fly ? for (i = 1 ; i < 6; i ++){ this.levelButton = this.add.button(this.levelButtonWidth * (i * 1.8), 100, 'levelButton' + i); this.levelButton.inputEnabled = true; this.levelButton.events.onInputDown.add(function(){ console.log('You Pressed LevelButton' + i); // pass this value to GameState }, this); } I have tried this.levelButton = this.add.button(this.levelButtonWidth * (i * 1.8), 100, 'levelButton' + i); but I keep getting a null reference any help would be appreciated Eric Link to comment Share on other sites More sharing options...
johncintron Posted December 18, 2016 Share Posted December 18, 2016 You're getting a null reference because this.levelButtonWidth is undefined. Also, you should use this syntax for your for loop: for (let i = 1; i < 6; i++) { // loop body } because it creates a new closure for each value of i in the loop. erich 1 Link to comment Share on other sites More sharing options...
erich Posted December 18, 2016 Author Share Posted December 18, 2016 3 minutes ago, johncintron said: You're getting a null reference because this.levelButtonWidth is undefined. Also, you should use this syntax for your for loop: for (let i = 1; i < 6; i++) { // loop body } because it creates a new closure for each value of i in the loop. thank you, so much !! Eric Link to comment Share on other sites More sharing options...
Recommended Posts