CocosCreator 拖动时判断是否拖到指定位置

2021-01-19  本文已影响0人  凡凡的小web

//判断矩形相交
https://blog.csdn.net/iningwei/article/details/90372031s
let rec1: cc.Rect = new cc.Rect(10, 10, 100, 50);//声明矩形区域
let rec2: cc.Rect = new cc.Rect(20, 20, 80, 80);
let intersecRec: cc.Rect;
rec1.intersection(intersecRec, rec2);//获得相交区域
let isIntersect: boolean = rec1.intersects(rec2);//判断是否相交

// 这样判断稍微碰到就触发
let rect = target.getBoundingBox()
let otherRect = squares[0].getBoundingBoxToWorld()
// if(rect.intersects(otherRect)){
// cc.log(1113244)
// }

// 这样判断只有手指移动进去才算,但是自己适配时不准
//判断点击的点是否在指定矩形内部
https://www.pianshen.com/article/3620193647/
if(!self.area_node.getBoundingBoxToWorld().contains(event.getLocation()))
{

    }

//如下修改
let dpos = target.parent.convertToNodeSpaceAR(new cc.Vec2(cc.winSize.width/2, cc.winSize.height/2))
let pos = target.parent.convertToNodeSpaceAR(e.getLocation())
let squares = this.squares_node
let index = target.index
//获取target节点在父容器的包围盒,返回一个矩形对象
let otherRect = squares[0].getBoundingBox()
pos.x -= dpos.x

    if(otherRect.contains(pos))
    {
        cc.log(1113244)
    }

// 多边形判断
https://docs.cocos.com/creator/2.3/manual/zh/physics/collision/collision-manager.html?h=cc.intersection
if (cc.Intersection.pointInPolygon(touchLoc, this.collider.world.points)) {
console.log("Hit!");
}

上一篇下一篇

猜你喜欢

热点阅读