Jump to content

How to access a button's callback function.


PixelProgrammer
 Share

Recommended Posts

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

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

@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

 Share

  • Recently Browsing   0 members

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