分享Creator点击事件透明穿透问题

:+1::+1:nice

onLucencyTouch里面,每次都ctx.drawImage,会不会有效率问题呢?

MASK :slightly_smiling: :grinning:

每次都是draw的一个像素,没有效率问题,网页跑的没问题目前

大哥,你这个在安卓系统无效啊,模拟器也跑不起来

这条语句浏览器支持,模拟器不支持。

这块代码也是浏览器支持,模拟器不支持

他这是用canvas的属性,你模拟器用OpenGL是不支持的!

楼主的思路是可以的!
其实通常做法应该是通过鼠标坐标来计算出点击的在哪个tile上,然后通过tile的row,col,来判断是否是选中了
这tile区域的物件!

先谢谢楼主分享,我按楼主的方法去做,但提示getComponent(…).spriteFrame.getTexture(…).getHtmlElementObj is not a function是什么原因呢?求楼主解救

1赞

我也遇到楼上的这个问题,有解决吗?

楼上不是说了OpenGL不支持了吗,透明区域的判断用Polygon Collider组件来判断

新手:slightly_smiling: 谢谢楼上的

this.listenerCallBack = cc.eventManager.addListener(listener, self.node);这句代码获得一个EventListener对象,然后才能用该对象调用下面的函数实现穿透,前面版本我都用得很顺手:
setTouchSwallow: function(isSwallowTouch){
this.listenerCallBack.setSwallowTouches(isSwallowTouch);
},
可现在1.6版本cc.eventManager.addListene被废除了,现在用哪种方式注册事件可以获得EventListener对象?我试过了好几个方式都不行。

现在1.6.1版本 self.node.getComponent(cc.Sprite).spriteFrame.getTexture().getHtmlElementObj(); 这里获取不到对象了怎么弄呢,

@panda大神 找了一天啊,终于找到_hitTest能实现了,建议放到常用XX的地方. @jare好硬.

新手引导主要就是修改touchMask拦截事件的node的_hitTest方法.mask反向遮罩
cc.Class({
extends: cc.Component,

properties: {
    // foo: {
    //    default: null,      // The default value will be used only when the component attaching
    //                           to a node for the first time
    //    url: cc.Texture2D,  // optional, default is typeof default
    //    serializable: true, // optional, default is true
    //    visible: true,      // optional, default is true
    //    displayName: 'Foo', // optional
    //    readonly: false,    // optional, default is false
    // },
    // ...
},

onLoad: function () {
},

onEnable: function () {
            this.touchMask.on(cc.Node.EventType.TOUCH_START,this.xxxx,this);
	this.touchMask._hitTest = this.customHitTest;
},

onDisable: function () {
},

onDestroy: function () {
    
},

// called every frame, uncomment this function to activate update callback
// update: function (dt) {

// },

customHitTest : function(point, listener) {
    // console.log("---->customHitTest");
    var w = this.width,
        h = this.height;
    var rect = cc.rect(0, 0, w, h);
    
    var Camera = cc.Camera;
    if (Camera && Camera.main && Camera.main.containsNode(this)) {
        point = Camera.main.getCameraToWorldPoint(point);
    }
    
    var trans = this.getNodeToWorldTransform();
    cc._rectApplyAffineTransformIn(rect, trans);
    var left = point.x - rect.x,
        right = rect.x + rect.width - point.x,
        bottom = point.y - rect.y,
        top = rect.y + rect.height - point.y;
    if (left >= 0 && right >= 0 && top >= 0 && bottom >= 0) {
        if (listener && listener.mask) {
            var mask = listener.mask;
            var parent = this;
            for (var i = 0; parent && i < mask.index; ++i, parent = parent.parent) {
            }
            // find mask parent, should hit test it
            if (parent === mask.node) {
                var comp = parent.getComponent(cc.Mask);
                return (comp && comp.enabledInHierarchy) ? !comp._hitTest(point) : true;//反向mask判断hitTest修改
            }
            // mask parent no longer exists
            else {
                listener.mask = null;
                return true;
            }
        }
        else {
            return true;
        }
    }else{
        return false;
    }
}

});

1赞

太复杂了, 能简单点吗?

技术贴

经常看到有人说_hitTest,我到现在还搞不懂_hitTest是什么东西,在我看来这个_hitTest貌似就是写在案例教程里面某个脚本里的一个方法名称而已,也就是用一个polygonCollider组件来判断点击位置是否在不规则区域内而已,怎么经常被说的好像是一种好像shader那样有独立概念的专有名词一样,有点高大上的样子。有高手来解释下_hitTest这个概念究竟是什么吗?

应该往这段代码里加像素点判断吗?有像素点判断的代码吗兄弟 我不会太加

CCC如何获取像素点呢?