2

In my simple project I use touch events to change a button states: in this case everything works fine

cc.eventManager.addListener(
{
    event: cc.EventListener.TOUCH_ALL_AT_ONCE,
    swallowTouches: false,
    onTouchesBegan: onTouchesBegan,
    onTouchesEnded: onTouchesEnded
}, this);

but in this case my onTouchEnded function have never been called:

cc.eventManager.addListener(
{
    event: cc.EventListener.TOUCH_ONE_BY_ONE,
    swallowTouches: false,
    onTouchBegan: onTouchBegan,
    onTouchEnded: onTouchEnded
}, this);

After some time with debugging I found "if" statement in cc.eventManager._onTouchEventCallback function:

} else if (listener._claimedTouches.length > 0
    && ((removedIdx = listener._claimedTouches.indexOf(selTouch)) != -1))
{

In my case "listener._claimedTouches" == 0 and the if statement denies calling onTouchMove and onTouchEnded listeners.

Does anyone have an idea why "listener._claimedTouches" == 0 and why such a thing is happened?

  • I dont see any problem in this code. Could you upload the entire code to see what's wrong? Also I usually refer to me function as this.nameFunction in the listener. I don't think this is the solution, because should be in scope, but just in case. Sometimes scopes in js are misleading. – Alex G. G. Sep 15 '15 at 11:12

1 Answers1

6

You should add a "return true;" statement at the end of your onTouchBegan. onTouchEnd would be triggered when onTouchBegan returns true.

var listener = cc.EventListener.create({
    event:cc.EventListener.TOUCH_ONE_BY_ONE,
    swallowTouches:false,
    onTouchBegan: function(touch, event) {
        return true;
    },
    onTouchEnded: function(touch, event) {
        //do sth.
    }
});
cc.eventManager.addListener(listener, this);
swen
  • 341
  • 1
  • 9