PixelProgrammer Posted February 17, 2017 Share Posted February 17, 2017 So I'm currently trying to implement controller support in my menu. The way I'm doing this right now is by pushing all the buttons into an array and when the player presses up or down on the dpad, they traverse through the array. But here's the issue. What do I use to access the callback parameter of the button? Essentially what I want to do is when the player reaches the button he wants to press in the array, I want to run the callback function. I know phaser does the automatically if you clicked the button. But what if I want to do it manually? if (controller.peressed.A_Button){ buttonArray[currentIndex].callbackFunction() } Link to comment Share on other sites More sharing options...
drhayes Posted February 17, 2017 Share Posted February 17, 2017 I'm not sure I understand your question. You don't want to use Phaser's method for attaching a callback to a button but want to use your own? You could do what Phaser does and save the callback and the callbackContext in your array as well, more or less like you've got in your code block there. Does that not work? Link to comment Share on other sites More sharing options...
PixelProgrammer Posted February 17, 2017 Author Share Posted February 17, 2017 @drhayes I'm sorry if I phrased the question badly. Let me clarify. I want to use phasers call back function. When I click the button I want the function to run. BUT I ALSO want to run the function whenever I want (For example when the user presses A on the controller). Now I know I can put all the callback functions in an array (and this is what I'm currently doing). But I feel this is bad code. Because if I were to rearrange buttons, I would have to rearrange the callback functions in the code in the same order as the buttons. I was just wondering if there was a way to access a button object's callback function. Such as playButton.callbackFunction()//runs play function. Where does phaser save the callback functions? Is it within the object itself or somewhere else? Link to comment Share on other sites More sharing options...
samme Posted February 17, 2017 Share Posted February 17, 2017 Phaser.Button.html#onInputUpHandler Phaser.Events.html#onInputUp → Phaser.Signal.html#dispatch Link to comment Share on other sites More sharing options...
drhayes Posted February 17, 2017 Share Posted February 17, 2017 The good news is the function you pass to the button's handler is just a function; you can call it whenever you want. Samme's right. onInputUp is a Phaser.Signal, and you could dispatch that signal the same way Phaser does. Link to comment Share on other sites More sharing options...
PixelProgrammer Posted February 17, 2017 Author Share Posted February 17, 2017 @samme Awesome! This is exactly what I wanted. Link to comment Share on other sites More sharing options...
PixelProgrammer Posted February 17, 2017 Author Share Posted February 17, 2017 @drhayes Yup. onInputUp is what I was looking for. Link to comment Share on other sites More sharing options...
Recommended Posts