PixelProgrammer

How to access a button's callback function.

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()
}

 

Share this post


Link to post
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?

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.