Cocos 3.4.0 通过触摸事件和射线检测进行滑动方块和停止方块的移动


import { _decorator, BoxCollider2D, Collider2D, Component, Contact2DType, director, ERaycast2DType, error, EventTouch, Input, input, instantiate, IPhysics2DContact, Layout, Node, PhysicsSystem2D, Prefab, resources, tween, UITransform, Vec2, Vec3 } from 'cc';
import { SingleMgr } from '../Managers/SingleMgr';
import EventManager from '../Managers/EventManager';
import { GAMEFUNC } from '../Tip/GameFunc';
const { ccclass, property } = _decorator;

@ccclass('UpFly')
export class UpFly extends Component {
    @property([Node])
    upNode: Node[] = [];
    @property([Node])
    downNode: Node[] = [];
    @property([Node])
    rightNode: Node[] = [];
    @property([Node])
    leftNode: Node[] = [];

    startPos: Vec2 = null;  //开始的位置
    endPos: Vec2 = null;  //结束位置
    moveSpeed: number = 200;  //移动速度
    //允许位移
    rightBool: boolean = false;
    leftBool: boolean = false;
    upBool: boolean = false;
    downBool: boolean = false;
    direction: string = null;
    start() {
        //this.YinQi(new Vec2(0, 0));
        SingleMgr.getInstance().isTouch = true;
        this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.on(Input.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
    }
    onTouchStart(event: EventTouch) {
        this.startPos = event.touch.getUILocation();
        // console.log("isTouch", SingleMgr.getInstance().isTouch);
        // console.log("direction", this.direction);
        // console.log("rightBool", this.rightBool);
        // console.log("leftBool", this.leftBool);
        // console.log("downBool", this.downBool);
        // console.log("upBool", this.upBool);
        // this.rightBool = true;
        // this.leftBool = true;
        // this.upBool = true;
        // this.downBool = true;
    }
    onTouchEnd(event: EventTouch) {

        //this.endPos = event.getUILocation();
    }

    onTouchMove(event: EventTouch) {

        if (SingleMgr.getInstance().isTouch) {
            this.endPos = event.touch.getUILocation();
            const distance = this.endPos.subtract(this.startPos);   //获取到结束触摸位置到开始触摸位置的一个向量(subtract向量的减法)
            //console.log("向量x", distance.x);
            //console.log("向量y", distance.y);
            const angle = Math.atan2(distance.y, distance.x) * 180 / Math.PI;   //偏移量的计算
            //console.log("角度", angle);
            this.direction = null;
            if (Math.abs(distance.x) > Math.abs(distance.y)) {
                if (distance.x > 0) {
                    if (angle > -45 && angle < 45) {
                        this.direction = "right";
                        //console.log("向右");
                    }
                } else {
                    if ((angle > -180 && angle < -135) || (angle > 135 && angle < 180)) {
                        this.direction = "left";
                        //console.log("向左");
                    }
                }
            } else {
                if (distance.y > 0) {
                    if (angle > 45 && angle < 135) {
                        this.direction = "up";
                        //console.log("向上");
                    }

                } else {
                    if (angle > -135 && angle < -45) {
                        this.direction = "down";
                        //console.log("向下");
                    }
                }
            }
            if (this.direction != null) {
                switch (this.direction) {
                    case "right":
                        for (let i = 0; i < this.rightNode.length; i++) {
                            this.RayThings(this.rightNode[i], 0, 14);
                        }
                        SingleMgr.getInstance().isTouch = false;
                        this.rightBool = true;
                        break;
                    case "left":
                        for (let i = 0; i < this.leftNode.length; i++) {
                            this.RayThings(this.leftNode[i], 0, -14);
                        }
                        SingleMgr.getInstance().isTouch = false;
                        this.leftBool = true;
                        break;
                    case "up":
                        for (let i = 0; i < this.upNode.length; i++) {
                            this.RayThings(this.upNode[i], 14, 0);
                        }
                        SingleMgr.getInstance().isTouch = false;
                        this.upBool = true;
                        break;
                    case "down":
                        for (let i = 0; i < this.downNode.length; i++) {
                            this.RayThings(this.downNode[i], -14, 0);
                        }
                        SingleMgr.getInstance().isTouch = false;
                        this.downBool = true;
                        break;
                    default:
                        break;
                }
            }
        }
    }

    //节点node目标射出射线的方向
    RayThings(node: Node, up?: number, right?: number): Boolean {
        let followMove = null;
        //console.log("射线检测", node.name);
        //起点位置
        let objs = new Vec2(node.getWorldPosition().x, node.getWorldPosition().y);
        //console.log("位置?" + objs);
        // 终点
        let obje = new Vec2(objs.x + right, objs.y + up);
        // 射线检测
        let results = PhysicsSystem2D.instance.raycast(objs, obje, ERaycast2DType.Any);

        if (results) {
            let result = results[0];
            if (result) {
                followMove = true;
                //console.log("碰撞体的信息", result.collider.node.name);
                //console.log(result.collider.node.getChildByName("name").getComponent(Label).string);
            }
        }
        else {
            followMove = false;
        }
        if (results.length >= 1) {
            //this.followMove = false;    //是否允许移动
        }
        //检测到目标才能移动
        return followMove;
    }
    //节点node目标射出射线的方向
    RayString(node: Node, up?: number, right?: number): Node {
        let name = null;
        //起点位置
        let objs = new Vec2(node.getWorldPosition().x, node.getWorldPosition().y);
        //console.log("位置?" + objs);
        // 终点
        let obje = new Vec2(objs.x + right, objs.y + up);
        // 射线检测
        let results = PhysicsSystem2D.instance.raycast(objs, obje, ERaycast2DType.Any);

        if (results) {
            let result = results[0];
            if (result) {
                name = result.collider.node;
                //console.log("碰撞体的信息", result.collider.node.name);
                //console.log(result.collider.node.getChildByName("name").getComponent(Label).string);
            }
        }
        else {
        }
        if (results.length >= 1) {
            name = results[0].collider.node;
            //this.followMove = false;    //是否允许移动
        }
        //检测到目标才能移动
        return name;
    }

    update(dt: number): void {
        //console.log("update", this.node.position);
        //一直发出射线
        if (this.upBool) {
            if (this.RayThings(this.upNode[0], 14, 0) || this.RayThings(this.upNode[1], 14, 0) || this.RayThings(this.upNode[2], 14, 0)) {
                GAMEFUNC.playEffect("hitWall");
                if (this.RayThings(this.upNode[0], 14, 0) && this.RayThings(this.upNode[1], 14, 0) && this.RayThings(this.upNode[2], 14, 0)) {
                    let nodeName = this.RayString(this.upNode[0], 14, 0);
                    let nodeName1 = this.RayString(this.upNode[1], 14, 0);
                    let nodeName2 = this.RayString(this.upNode[2], 14, 0);
                    if (nodeName.name == this.node.name || nodeName1.name == this.node.name || nodeName2.name == this.node.name) {
                        // console.log("碰到自己了");
                        this.ChangeBox(nodeName);
                    }
                }
                //有障碍物不允许移动
                //this.moveSpeed = 0;
                this.upBool = false;
                SingleMgr.getInstance().isTouch = true;
                // console.log("upBool动了吗");
                return;
            }
            if (!this.RayThings(this.upNode[0], 14, 0) && !this.RayThings(this.upNode[1], 14, 0) && !this.RayThings(this.upNode[2], 14, 0)) {
                //console.log("upBool动了");
                //没有障碍物允许移动    
                this.node.translate(this.node.up.multiplyScalar(this.moveSpeed * dt));
            }
        }
        if (this.downBool) {
            if (this.RayThings(this.downNode[0], -14, 0) || this.RayThings(this.downNode[1], -14, 0) || this.RayThings(this.downNode[2], -14, 0)) {
                GAMEFUNC.playEffect("hitWall");
                if (this.RayThings(this.downNode[0], -14, 0) && this.RayThings(this.downNode[1], -14, 0) && this.RayThings(this.downNode[2], -14, 0)) {
                    let nodeName = this.RayString(this.downNode[0], -14, 0);
                    let nodeName1 = this.RayString(this.downNode[1], -14, 0);
                    let nodeName2 = this.RayString(this.downNode[2], -14, 0);
                    if (nodeName.name == this.node.name || nodeName1.name == this.node.name || nodeName2.name == this.node.name) {
                        // console.log("碰到自己了");
                        this.ChangeBox(nodeName);
                    }
                }
                //有障碍物不允许移动               
                SingleMgr.getInstance().isTouch = true;
                this.downBool = false;
                // console.log("downBool动了吗");;
                return;
            }
            if (!this.RayThings(this.downNode[0], -14, 0) && !this.RayThings(this.downNode[1], -14, 0) && !this.RayThings(this.downNode[2], -14, 0)) {
                //没有障碍物允许移动
                this.node.translate(this.node.up.negative().multiplyScalar(this.moveSpeed * dt));
            }
        }
        if (this.rightBool) {
            if (this.RayThings(this.rightNode[0], 0, 14) || this.RayThings(this.rightNode[1], 0, 14) || this.RayThings(this.rightNode[2], 0, 14)) {
                GAMEFUNC.playEffect("hitWall");
                if (this.RayThings(this.rightNode[0], 0, 14) && this.RayThings(this.rightNode[1], 0, 14) && this.RayThings(this.rightNode[2], 0, 14)) {
                    let nodeName = this.RayString(this.rightNode[0], 0, 14);
                    let nodeName1 = this.RayString(this.rightNode[1], 0, 14);
                    let nodeName2 = this.RayString(this.rightNode[2], 0, 14);
                    if (nodeName.name == this.node.name || nodeName1.name == this.node.name || nodeName2.name == this.node.name) {
                        // console.log("碰到自己了");
                        this.ChangeBox(nodeName);
                    }
                }
                //有障碍物不允许移动
                SingleMgr.getInstance().isTouch = true;
                this.rightBool = false;
                return;
            }
            if (!this.RayThings(this.rightNode[0], 0, 14) && !this.RayThings(this.rightNode[1], 0, 14) && !this.RayThings(this.rightNode[2], 0, 14)) {
                this.node.translate(this.node.right.multiplyScalar(this.moveSpeed * dt));
            }
        }
        if (this.leftBool) {
            if (this.RayThings(this.leftNode[0], 0, -14) || this.RayThings(this.leftNode[1], 0, -14) || this.RayThings(this.leftNode[2], 0, -14)) {
                GAMEFUNC.playEffect("hitWall");
                if (this.RayThings(this.leftNode[0], 0, -14) && this.RayThings(this.leftNode[1], 0, -14) && this.RayThings(this.leftNode[2], 0, -14)) {
                    let nodeName = this.RayString(this.leftNode[0], 0, -14);
                    let nodeName1 = this.RayString(this.leftNode[1], 0, -14);
                    let nodeName2 = this.RayString(this.leftNode[2], 0, -14);
                    if (nodeName.name == this.node.name || nodeName1.name == this.node.name || nodeName2.name == this.node.name) {
                        this.ChangeBox(nodeName);
                    }
                }
                SingleMgr.getInstance().isTouch = true;
                this.leftBool = false;
                return;
            }
            if (!this.RayThings(this.leftNode[0], 0, -14) && !this.RayThings(this.leftNode[1], 0, -14) && !this.RayThings(this.leftNode[2], 0, -14)) {
                this.node.translate(this.node.right.negative().multiplyScalar(this.moveSpeed * dt));
            }
        }
    }
    //合成不同的盒子
    ChangeBox(otherNode: Node) {
        switch (this.node.name) {
            case "引气":
                this.node.parent.getComponent(Layout).enabled = false;
                var pos: Vec3 = null;
                if (this.node && otherNode) {
                    pos = otherNode.position;
                    GAMEFUNC.playEffect("xiaoChu");
                    otherNode.active = false;
                    this.node.active = false;
                    setTimeout(() => {
                        otherNode.destroy();
                        this.node.destroy();
                    }, 1000);
                    resources.load("Prefab/UpGame/凝气" + "", Prefab, (error, prefab) => {
                        let box = instantiate(prefab);  //生成预设体
                        console.log("凝气");
                        box.setParent(this.node.parent);  //设置父节点
                        box.setPosition(pos);
                    });
                }
                break;
            case "凝气":
                this.node.parent.getComponent(Layout).enabled = false;
                var pos: Vec3 = null;
                if (this.node && otherNode) {
                    pos = otherNode.position;
                    GAMEFUNC.playEffect("xiaoChu");
                    otherNode.active = false;
                    this.node.active = false;
                    setTimeout(() => {
                        otherNode.destroy();
                        this.node.destroy();
                    }, 1000);
                    resources.load("Prefab/UpGame/化气" + "", Prefab, (error, prefab) => {
                        let box = instantiate(prefab);  //生成预设体
                        console.log("化气");
                        box.setParent(this.node.parent);  //设置父节点
                        box.setPosition(pos);
                    });
                }
                break;
            case "化气":
                this.node.parent.getComponent(Layout).enabled = true;
                var pos: Vec3 = null;
                if (this.node && otherNode) {
                    pos = otherNode.position;
                    GAMEFUNC.playEffect("xiaoChu");
                    otherNode.active = false;
                    this.node.active = false;
                    setTimeout(() => {
                        otherNode.destroy();
                        this.node.destroy();
                    }, 1000);

                    resources.load("Prefab/UpGame/凝丹" + "", Prefab, (error, prefab) => {
                        let box = instantiate(prefab);  //生成预设体
                        console.log("凝丹");
                        box.setParent(this.node.parent);  //设置父节点
                        box.setPosition(pos);
                    });
                }
                break;
            case "凝丹":    //升级,改变样式
                this.node.parent.getComponent(Layout).enabled = false;
                var pos: Vec3 = null;
                if (this.node && otherNode) {
                    pos = otherNode.position;
                    GAMEFUNC.playEffect("xiaoChu");
                    otherNode.active = false;
                    this.node.active = false;
                    setTimeout(() => {
                        otherNode.destroy();
                        this.node.destroy();
                    }, 1000);

                    resources.load("Prefab/UpGame/韵丹" + "", Prefab, (error, prefab) => {
                        let box = instantiate(prefab);  //生成预设体
                        console.log("韵丹");
                        box.setParent(this.node.parent);  //设置父节点
                        box.setPosition(pos);
                    });
                }
                break;
            case "韵丹":
                this.node.parent.getComponent(Layout).enabled = true;
                var pos: Vec3 = null;
                if (this.node && otherNode) {
                    pos = otherNode.position;
                    GAMEFUNC.playEffect("xiaoChu");
                    otherNode.active = false;
                    this.node.active = false;
                    setTimeout(() => {
                        otherNode.destroy();
                        this.node.destroy();
                    }, 1000);

                    resources.load("Prefab/UpGame/破丹" + "", Prefab, (error, prefab) => {
                        let box = instantiate(prefab);  //生成预设体
                        console.log("破丹");
                        box.setParent(this.node.parent);  //设置父节点
                        box.setPosition(pos);
                    });
                }
                break;
            case "破丹"://升级,改变样式
                this.node.parent.getComponent(Layout).enabled = false;
                var pos: Vec3 = null;
                if (this.node && otherNode) {
                    pos = otherNode.position;
                    GAMEFUNC.playEffect("xiaoChu");
                    otherNode.active = false;
                    this.node.active = false;
                    setTimeout(() => {
                        otherNode.destroy();
                        this.node.destroy();
                    }, 1000);

                    resources.load("Prefab/UpGame/元婴" + "", Prefab, (error, prefab) => {
                        let box = instantiate(prefab);  //生成预设体
                        console.log("元婴");
                        box.setParent(this.node.parent);  //设置父节点
                        box.setPosition(pos);
                    });
                }
                break;
            case "元婴":
                this.node.parent.getComponent(Layout).enabled = false;
                var pos: Vec3 = null;
                if (this.node && otherNode) {
                    pos = otherNode.position;
                    GAMEFUNC.playEffect("xiaoChu");
                    otherNode.active = false;
                    this.node.active = false;
                    setTimeout(() => {
                        otherNode.destroy();
                        this.node.destroy();
                    }, 1000);

                    resources.load("Prefab/UpGame/养神" + "", Prefab, (error, prefab) => {
                        let box = instantiate(prefab);  //生成预设体
                        console.log("养神");
                        box.setParent(this.node.parent);  //设置父节点
                        box.setPosition(pos);
                    });
                }
                break;
            case "养神":
                this.node.parent.getComponent(Layout).enabled = false;
                var pos: Vec3 = null;
                if (this.node && otherNode) {
                    pos = otherNode.position;
                    GAMEFUNC.playEffect("xiaoChu");
                    otherNode.active = false;
                    this.node.active = false;
                    setTimeout(() => {
                        otherNode.destroy();
                        this.node.destroy();
                    }, 1000);

                    resources.load("Prefab/UpGame/分神" + "", Prefab, (error, prefab) => {
                        let box = instantiate(prefab);  //生成预设体
                        console.log("分神");
                        box.setParent(this.node.parent);  //设置父节点
                        box.setPosition(pos);
                    });
                }
                break;
            case "分神":
                this.node.parent.getComponent(Layout).enabled = false;
                var pos: Vec3 = null;
                if (this.node && otherNode) {
                    pos = otherNode.position;
                    GAMEFUNC.playEffect("xiaoChu");
                    otherNode.active = false;
                    this.node.active = false;
                    setTimeout(() => {
                        otherNode.destroy();
                        this.node.destroy();
                    }, 1000);
                    resources.load("Prefab/UpGame/窥虚" + "", Prefab, (error, prefab) => {
                        let box = instantiate(prefab);  //生成预设体
                        console.log("窥虚");
                        box.setParent(this.node.parent);  //设置父节点
                        box.setPosition(pos);
                    });
                }
                break;
            case "窥虚":
                this.node.parent.getComponent(Layout).enabled = true;
                var pos: Vec3 = null;
                if (this.node && otherNode) {
                    pos = otherNode.position;
                    GAMEFUNC.playEffect("xiaoChu");
                    otherNode.active = false;
                    this.node.active = false;
                    setTimeout(() => {
                        otherNode.destroy();
                        this.node.destroy();
                    }, 1000);
                    resources.load("Prefab/UpGame/洞虚" + "", Prefab, (error, prefab) => {
                        let box = instantiate(prefab);  //生成预设体
                        console.log("洞虚");
                        box.setParent(this.node.parent);  //设置父节点
                        box.setPosition(pos);
                    });
                }
                break;
            case "洞虚"://升级,改变样式
                this.node.parent.getComponent(Layout).enabled = false;
                var pos: Vec3 = null;
                if (this.node && otherNode) {
                    pos = otherNode.position;
                    GAMEFUNC.playEffect("xiaoChu");
                    otherNode.active = false;
                    this.node.active = false;
                    setTimeout(() => {
                        otherNode.destroy();
                        this.node.destroy();
                    }, 1000);
                    resources.load("Prefab/UpGame/化虚" + "", Prefab, (error, prefab) => {
                        let box = instantiate(prefab);  //生成预设体
                        console.log("化虚");
                        box.setParent(this.node.parent);  //设置父节点
                        box.setPosition(pos);
                    });
                }
                break;
            case "化虚":
                this.node.parent.getComponent(Layout).enabled = false;
                var pos: Vec3 = null;
                if (this.node && otherNode) {
                    pos = otherNode.position;
                    GAMEFUNC.playEffect("xiaoChu");
                    otherNode.active = false;
                    this.node.active = false;
                    setTimeout(() => {
                        otherNode.destroy();
                        this.node.destroy();
                    }, 200);

                    resources.load("Prefab/UpGame/飞升" + "", Prefab, (error, prefab) => {
                        let box = instantiate(prefab);  //生成预设体
                        console.log("飞升");
                        box.setParent(this.node.parent);  //设置父节点
                        box.setPosition(pos);
                    });
                }
                break;
            case "飞升":
                console.log("游戏胜利");
                GAMEFUNC.playEffect("xiaoChu");
                otherNode.active = false;
                this.node.active = false;
                setTimeout(() => {
                    otherNode.destroy();
                    this.node.destroy();
                }, 200);
                EventManager.getInstance().emit("finishGame", []);
                setTimeout(() => {
                    if (this.node.parent.parent) {
                        this.node.parent.parent.destroy();
                    }
                }, 200);
                break;
            default:
                break;
        }
    }
}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值