cocos实现按钮多种状态监听

 var listener=cc.EventListener.create({
            event:cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches:true,
            onTouchBegan:function(touch,event){
                if(that.reachTarget)return false; //到达位置后不能移动
                var target=event.getCurrentTarget();
                var locationInNode=target.convertToNodeSpace(touch.getLocation());
                var s=target.getContentSize();
                var rect=cc.rect(0,0, s.width, s.height);
                if(cc.rectContainsPoint(rect,locationInNode)){
                    target.setZOrder(10);
                    return true;
                }
                return false;
            },
            onTouchMoved:function(touch,event){
                var target=event.getCurrentTarget();
                var delta=touch.getDelta();
                target.x+=delta.x;
                target.y+=delta.y;
            },
            onTouchEnded:function(touch,event) {
                var target = event.getCurrentTarget();
                if (cc.pDistance(target.getPosition(), that._targetPoint) < 50) {
                    target.setPosition(that._targetPoint);
                    that.reachTarget = true;
                    that._callHandler();
                }

            }
onTouchBegan

只有当onTouchBegan的返回值是true时才执行后面的两个触摸事件

onTouchMoved
按钮的移动事件,当按钮移动时,就执行里面的方法

onTouchEnded
触摸事件结束,也就是手指松开时  ,执行该方法

你可能感兴趣的:(cocos实现按钮多种状态监听)