cocosCreator中的碰撞检测和触摸事件

const { ccclass, property } = cc._decorator;
@ccclass
export default class BallClass extends cc.Component {
    @property
    text: string = 'Ball';

    @property(cc.Node)
    Ball: cc.Node = null;

    @property
    weight: number = null;

    /** 物体的默认排序 */
    @property
    partIndex: number = null;

    /** 目标节点 */
    // @property(cc.Node)
    // aimParentNode: cc.Node = null;

    @property(cc.Component)
    game: any = null;

    onLoad() {

    }

    start() {

    }

    init(game: cc.Component) {
        this.game = game;
        this.registerEvent(this.node);
    }

    registerEvent(ballNode: cc.Node) {
        ballNode.on(cc.Node.EventType.TOUCH_START, this.touchStart, this);
        ballNode.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this);
        ballNode.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this);
    }

    unRegisterEvent(ballNode: cc.Node) {
        ballNode.off(cc.Node.EventType.TOUCH_START, this.touchStart, this);
        ballNode.off(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this);
        ballNode.off(cc.Node.EventType.TOUCH_END, this.touchEnd, this);
    }

    touchStart(event) {
        console.log('--触摸--start');
        if (!this.game.cheakRunning()) {
            return;
        }
        // let sp = this.getComponent(cc.Sprite);
        // console.log('sp.node:', sp.node)

        let Part = this.node.parent.getChildByName('Part');
        Part.opacity = 255;
        let target = event.target;
        target.scale = 1.5;
        target.zIndex = 1;
    }

    touchMove(event: cc.Event.EventTouch) {
        console.log('--触摸--move');
        if (!this.game.cheakRunning()) {
            return;
        }
        var posX = event.touch.getLocationX() - 640;
        var posY = event.touch.getLocationY() - 360;
        let target = event.target;
        let delta = event.touch.getDelta();
        target.x += delta.x;
        target.y += delta.y;
        target.rotation = 0;
        target.zIndex = 1;
    }

    touchEnd(event) {
        console.log('--触摸--end', this);
        if (false && !this.game.cheakRunning()) {
            // 回归原位
            console.log('---没时间了--');

            this.backToOriginal(event.target);
            return;
        }
        console.log('end-pos', event);
        this.checkCollection(event.target);
    }

    checkCollection(ballNode: cc.Node) {
        console.log('--检测碰撞--', ballNode);
        console.log('--世界坐标系--', ballNode.convertToWorldSpace(ballNode.getPosition()))
        let target: cc.Node = ballNode;
        let targetParent = target.parent;
        let x = target.x + targetParent.x + targetParent.parent.x;
        let y = target.y + targetParent.y + targetParent.parent.y;

        let ball = target.getComponent('Ball');
        console.log(ball);
        let aimNode = this.game.coacheList[ball.weight];

        let aimX = aimNode.x + aimNode.parent.x + aimNode.parent.parent.x;
        let aimY = aimNode.y + aimNode.parent.y + aimNode.parent.parent.y;
        let distance = Math.sqrt((x - aimX) * (x - aimX) + (y - aimY) * (y - aimY))

        console.log({ aimX, aimY, targetX: target.x, targetY: target.y, distance, x, y })

        if (distance < (target.height + ballNode.height) / 2) {
            if (!target.isChildOf(aimNode)) {
                target.parent = aimNode;
                target.position = cc.v2(0, -10);
                target.scale = 1;
                target.zIndex = 1;
                this.unRegisterEvent(target);
                this.game.addScore();
            }
        } else {
            this.backToOriginal(ballNode);
        }

    }

    /**
     * 回到原来的位置
     * @param ballNode 
     */
    backToOriginal(ballNode: cc.Node) {
        let ball: BallClass = ballNode.getComponent('Ball');
        let moveTo = cc.moveTo(0.3, cc.v2(0, 0));    // 此处默认0点位置
        let callFunc = cc.callFunc(() => {
            let Part = ballNode.parent.getChildByName('Part');
            Part.opacity = 0;
        });
        let sequence = cc.sequence(moveTo, callFunc);
        ballNode.runAction(sequence);
        ballNode.scale = 1;
        ballNode.zIndex = 1;


    }

    // update (dt) {}
}

猜你喜欢

转载自www.cnblogs.com/chzh999/p/9456964.html