kbmonkey Posted February 16, 2014 Share Posted February 16, 2014 Hi all! I have an interesting question today: I am making a game that requires me to build some buttons that use the same callback: this.buyButtons = []; for (var n=0; n<8; n++) { var y = 10 + (n * 35); this.buyButtons.push(this.add.button(10, y, 'buyButton', this.dealButton, this, 2, 1, 0)); }The buttons perform the same task with a modifier counter, how can I pass an "Index" or similar value to the Callback so it knows which of the 8 buttons was clicked? dealButton: function (pointer) { // which button was clicked this time? // setting a custom property value on each button object would work // but then I need a reference to the button to test against. },Thank you Link to comment Share on other sites More sharing options...
Zaidar Posted February 16, 2014 Share Posted February 16, 2014 Just from my head, would it work ?this.buyButtons = [];this.tempButtons = [];for (var n=0; n<8; n++){ var y = 10 + (n * 35); var button = this.add.button(10, y, 'buyButton', this.dealButton, this, 2, 1, 0); button.nameButton = "button" + n; this.buyButtons.push(button);}// laterdealButton = function(pointer){ this.tempButtons.push(pointer.nameButton); if(pointer.nameButton == "bouton1"){ }} Link to comment Share on other sites More sharing options...
rich Posted February 16, 2014 Share Posted February 16, 2014 Sprites (and therefore Buttons) have a name property. So set that to something useful And also the first parameter sent to dealButton will be a reference to the button itself, not the pointer (that's the 2nd parameter). So modify your function to: dealButton: function (button, pointer) { console.log('you clicked', button.name); }, Zaidar 1 Link to comment Share on other sites More sharing options...
kbmonkey Posted February 16, 2014 Author Share Posted February 16, 2014 Ah thank you very much to both of you. Setting a custom property on the button object fixed it. And thanks for that details about the 2 parameters Rich, I was looking for the callback signature in the docs but guess I am still learning the JavaScript way of doing things. I would make waffles for both of you Link to comment Share on other sites More sharing options...
Recommended Posts