Jump to content

help with a loop


erich
 Share

Recommended Posts

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

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

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...