鳕鱼天空

This is Mr Wang's Tech Blog.

cocos 通过碰撞边缘判断非常规多边形按钮

     // 不规则形状通过碰撞边缘检测是否在有效区域内
            let target = this.node;
            let pos = target.convertToNodeSpaceAR(e.getLocation())
            var polygonCollider = target.getComponent(cc.PolygonCollider);
            //pos.x -= target.getContentSize().width / 2;   // 原始示例中由减去宽度和高度的设定,实测不需要
            //pos.y -= target.getContentSize().height / 2;
            if (!cc.Intersection.pointInPolygon(pos, polygonCollider.points)) {
                return;
            }

以上是多边形按钮


var Global = require("Global");

cc.Class({
    extends: cc.Component,

    properties: {
        touched: false,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart.bind(this));
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnd.bind(this));
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove.bind(this));
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEnd.bind(this));
    },

    onDestory() {

    },

    touchStart(e) {

        // 不规则形状通过碰撞边缘检测是否在有效区域内
        let target = this.node;
        let pos = target.convertToNodeSpaceAR(e.getLocation())
        var polygonCollider = target.getComponent(cc.PolygonCollider);
        //pos.x -= target.getContentSize().width / 2;   // 原始示例中由减去宽度和高度的设定,实测不需要
        //pos.y -= target.getContentSize().height / 2;
        if (!cc.Intersection.pointInPolygon(pos, polygonCollider.points)) {
            return;
        }
        this.touched = true;
        // 切换选中状态
        if (Global.selectedBoard != undefined && this.node != Global.selectedBoard) {
            Global.selectedBoard.opacity = 255;
        }
        Global.selectedBoard = this.node;
        this.node.opacity = 180;    // 选中状态半透明
        this.node.zIndex = 1;   // 最前
    },
    touchMove(e) {
        if (this.touched) {
            // 开始移动
            let delta = e.getDelta();
            this.node.x += delta.x;
            this.node.y += delta.y;
        }
    },
    touchEnd(e) {
        this.touched = false;
        this.node.zIndex = 0;
    },
});

以上是不规则图形按住拖动

添加评论

Loading