Jump to content

Phaser.Button callback access?


NewGuy
 Share

Recommended Posts

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

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

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);

 

Link to comment
Share on other sites

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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