const {ccclass, property} = cc._decorator;

@ccclass
export default class touch extends cc.Component {
    onLoad () {
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouch, this)
        this.node._hitTest = this.hitTest.bind(this)
    }

    hitTest(point){
        var locationInNode = this.node.convertToNodeSpace(point);
        var size = this.node.getContentSize();
        let sprite = this.node.getComponent(cc.Sprite)
        if(sprite){
            var imgObj = sprite.spriteFrame.getTexture().getHtmlElementObj();
            if(this.onLucencyTouch(imgObj, locationInNode.x, size.height-locationInNode.y)){
                return true
            }else{
                return false
            }
        }
        return false
    }

    onLucencyTouch(img, x, y){
        var cvs = document.createElement("canvas");
        var ctx = cvs.getContext('2d');
        cvs.width = 1;
        cvs.height = 1;
        ctx.drawImage(img,x,y,1,1,0,0,1,1);
        var imgdata = ctx.getImageData(0,0,1,1);
        return imgdata.data[3];
    }

    onTouch(touch){
        console.log("----------", this.node.name)
    }
}

 

相关文章:

  • 2021-12-25
  • 2021-07-02
  • 2022-12-23
  • 2022-12-23
  • 2021-07-14
  • 2021-05-31
  • 2021-11-14
  • 2022-12-23
猜你喜欢
  • 2021-08-27
  • 2021-10-04
  • 2022-12-23
  • 2022-12-23
  • 2021-09-21
  • 2021-09-19
相关资源
相似解决方案