NewGuy Posted March 7, 2017 Share Posted March 7, 2017 Probably a silly question, I'm wondering if there's a way to get a Phaser.Button to fire off it's callback externally? I'm basically writing a wee class with a progress bar that attaches to Phaser.Button's and when the progress bar is full, I'd like it to provoke the button to call it's callback. Any ideas? (My next plan was making an extension of Phaser.button that's hold a reference to the button's callback but I'd like to avoid doing that) Link to comment Share on other sites More sharing options...
samme Posted March 7, 2017 Share Posted March 7, 2017 It's a little weird because the Button expects a Pointer, but you can try btn.onInputUp.dispatch(btn, btn.game.input.activePointer, false) NewGuy 1 Link to comment Share on other sites More sharing options...
NewGuy Posted March 7, 2017 Author Share Posted March 7, 2017 Hey, samme. I managed to get as far as 'btn.onInputUp.dispatch()' by looking at the source. Unfortunately yours didn't work, hmmm if (callback !== null) { this.onInputUp.add(callback, callbackContext); } And as far as I could tell it looks like you'd just need to call dispatch since it has the function and the context right? Something missing Link to comment Share on other sites More sharing options...
NewGuy Posted March 7, 2017 Author Share Posted March 7, 2017 Pardon me, it's a long story but I'm working with a workmate's old code and he wrote a middle class that changed the signal used for calling the callback. Samme's method does work. Link to comment Share on other sites More sharing options...
Arcanorum Posted March 7, 2017 Share Posted March 7, 2017 If the function to use as a callback is declared somewhere then just call it. What I think you are doing: var yourButton = game.add.button(x, y, 'frame', function(btn){ // Button functionality code here. }, this); What I think you should be doing: var yourCallback = function(btn){ // Your code here. } var yourButton = game.add.button(x, y, 'frame', yourCallback, this); // The function used as a callback can still be called manually, even passing in the button that you want. yourCallback(yourButton); NewGuy 1 Link to comment Share on other sites More sharing options...
NewGuy Posted March 7, 2017 Author Share Posted March 7, 2017 Not quite @Arcanorum! It was a weird scenario. I was building a class that took any Phaser.Button as a parameter and draws the progress arc around the button. The idea was to allow you to tap the button or do another action that would fill the progress bar and fire off the targeted button's callback when the bar was full. My original issue was I didn't have visibility of the targeted button's callback BUT I found out through reading the Phaser source that the callback is added to the inputUp signal be default. <wall/> Link to comment Share on other sites More sharing options...
Recommended Posts