Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

callback functions for buttons in a group.


kemz
 Share

Recommended Posts

I created 18 buttons and each button should point to specific callback function ( play a specific level).  I was able to create the  18 buttons and added them to  a button  group called   world1LevelGroup. The problem is  i can't make  each button to point to a specific function. The code is shown below. Any help please? 

 

 

 x = this.game.world.centerX; y = this.game.world.centerY; level = [] ; world1LevelGroup = this.add.group();for( var i = 1; i <=3; i++){         for( var j = 1; j <=6; j++){                           if( j == 1){n= i;}            if( j == 2){n= i + 3;}            if( j == 3){n= i+6;}            if( j == 4){n= i+9;}            if( j == 5){n= i+12;}            if( j == 6){n= i+15;}                      level[n]  =  this.add.button(i*(x*0.5), j*(y/1.8), 'levelbt',  this.playLevel, this);         level[n].anchor.setTo(0.5);         level[n].scale.setTo(0.5,0.5);                 if( i >1 ||j>1){                     level[n].alpha = 0.4;                      level[n].input.enabled = false;                 }         this.add.tween(level[n]).from( { x: 2*x + 200}, 1000, Phaser.Easing.Bounce.Out, true);         world1LevelGroup.add(level[n]);         }         }

Thanks in advance.

 

Link to comment
Share on other sites

You can create function references dynamically. Say for example your functions are called "playLevel1", "playLevel2", and so on.. you could do:

this.add.button(i*(x*0.5), j*(y/1.8), 'levelbt',  this['playLevel' + c], this);

Where 'c' is a number, so you could create the buttons in a for loop.

 

Alternatively though you could make each button point to the same function, and then just check which button invoked that function. The button is passed as an argument to the function, at which point you could check to see which one it was.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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