Responding to ccui.Widget callbacks

I am trying to create buttons that will function as a D-pad to allow the player to move the character using a ccui.Button. I am trying to respond to the methods onPressStateChangedToNormal() and onPressStateChangedToPressed(). I however cannot figure out how to respond to the callbacks. My current attempt is by using the method as a variable and assigning my own function, which results in cc.log not being called.

My code is as follows:

this.btnUp = new ccui.Button();
this.btnUp.loadTextureNormal(res.arrowUp_png, null);    
this.btnUp.setPosition(100, 100);
this.btnUp.onPressStateChangedToPressed = function(){ cc.log("btnUp pressed!"); };

The value of res.arrowUp_png is "res/arrow-up.png".

If a different type of button is more suited for my use case, I’d be happy to switch implementations. For now, I either want to log a message every time the game updates while the button is pressed, or to log a message when the button has been pressed and released.

Hi,

I didn’t try with “onPressStateChangedToPressed” method. Instead of that i have added touch event listener to button.

//Add listener to button
this.btnUp.addTouchEventListener(this.touchEvent, this);

//touchEvent method will call when we press on menu button. Am using following code.
touchEvent: function (sender, type) {
switch (type) {
case ccui.Widget.TOUCH_BEGAN:
this._topDisplayLabel.setString(“Touch Down”);
break;

        case ccui.Widget.TOUCH_MOVED:
            this._topDisplayLabel.setString("Touch Move");
            break;

        case ccui.Widget.TOUCH_ENDED:
            this._topDisplayLabel.setString("Touch Up");
            break;

        case ccui.Widget.TOUCH_CANCELED:
            this._topDisplayLabel.setString("Touch Cancelled");
            break;

        default:
            break;
    }
}

I found nice way to simulate a similar behaviour as onPressStateChangedToPressed

    button.addTouchEventListener(function(button, event) {
      if (button.isHighlighted()) {
        // You button has the pressed texture
      }
      else {
        // You button has the normal texture
      }
    }, this);