Cocosd2d-js 实现模态对话框

使用模态对话框主要是吞噬点击事件,让点击不会透传导底部,新建窗口都继承自模态对话框,也可以创建子类便于实现打开和关闭的动画效果

/**
 * Created by shake on 16/4/7.
 */
"use strict";

var ModalDialogLayer = cc.Layer.extend({
    m_touchListener:null,

    ctor:function(){
        this._super();
        var touchListener = {
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: true,
            onTouchBegan: this.onTouchBegan
        };
        cc.eventManager.addListener(touchListener, this);
        this.m_touchListener = touchListener;
    },

    isTouchInside: function (owner, touch) {
        if(!owner || !owner.getParent()){
            return false;
        }
        var touchLocation = touch.getLocation();
        touchLocation = owner.getParent().convertToNodeSpace(touchLocation);
        return cc.rectContainsPoint(owner.getBoundingBox(), touchLocation);
    },

    onTouchBegan: function (touch, event) {
        var target = event.getCurrentTarget();
        // if(!target.isVisible() || (!this.isTouchInside(target,touch))){
        if(!target.isVisible() ){
            return false;
        }

        return true;
    },
});

module.exports = ModalDialogLayer;


你可能感兴趣的:(cocos2d,对话框)