Cocos3.4.2版本 获取手指滑动到的区域的Label文本并记录文本内容进行判定操作



import { _decorator, Component, Event, EventTouch, find, Input, input, Label, Node, Rect, sys, TextAsset, tween, UIOpacity, UITransform, Vec2, Vec3 } from 'cc';
import { JsonMgr } from '../Managers/JsonMgr';
import { strings } from '../Struct/GameStruct';
import { GAMEFUNC } from '../Tip/GameFunc';
import { SingleMgr } from '../Managers/SingleMgr';

const { ccclass, property } = _decorator;

///用于获取手指滑动区域的文本并记录文本内容
@ccclass('Getstring')
export class Getstring extends Component {
    label: Label;

    b: boolean = true;
    op:UIOpacity;
    onLoad() {
        let a = GAMEFUNC.Search(this.node, "name");
        this.label = a.getComponent(Label);
        this.op=a.getComponent(UIOpacity);
        //this.label = this.node.getChildByName("name").getComponent(Label);
        //input.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);
        //input.on(Input.EventType.TOUCH_START, this.onTouchStart, this);
        input.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.parent.parent.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        
        //this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        //JsonMgr.getInstance().SetJsonOne(strings.InputTxt,"");  //初始化数据


    }
    start() {

    }

    update(deltaTime: number) {

    }

    onTouchEnd(event: EventTouch) {
        
        this.b = true;
        //JsonMgr.getInstance().SetJsonOne(strings.InputTxt,"");    //清空数据
        //判断内容
        //JsonMgr.getInstance().SetJson(strings.InputTxt);  
        SingleMgr.getInstance().csvKnow = true;  //允许查看  
        
        // if(SingleMgr.getInstance().ShowText){
        //     console.log("渐变!!!");
        //     tween(this.op)
        //     .to(1, {opacity: 0})
        //     .to(1, {opacity: 255})
        //     .start();
        //     SingleMgr.getInstance().ShowText=false;
        // }   
    }

    onTouchMove(event: EventTouch) {
        const x=event.touch.getUILocationX();
        const y=event.touch.getUILocationY();

        //const touchPos = event.getLocation();
        //const localPos = this.node.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(touchPos.x, touchPos.y));
        const localPos = this.node.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(x, y));
        const size = this.node.getComponent(UITransform).contentSize;
        const rect = new Rect(-size.width / 2, -size.height / 2, size.width, size.height);
        //console.log("位置更新:"+localPos);
        if (rect.contains(new Vec2(localPos.x, localPos.y))) {
            const text = this.label.string;
            if (this.b) {
                //动画效果
                tween(this.node)
                    .to(0.2, { scale: new Vec3(1.2, 1.2, 1.2) })
                    .to(0.1, { scale: new Vec3(1, 1, 1) })
                    .start();
                let inputText = JsonMgr.getInstance().GetJson(strings.InputTxt);
                // 在这里操作获取到的文本内容                
                JsonMgr.getInstance().SetJsonOne(strings.InputTxt, inputText + text);//字符串连接
                this.b = false;
                let te = JsonMgr.getInstance().GetJson(strings.InputTxt);  //字符串赋值
                console.log(te);
            }

        }
    }

}

    //递归获取子物体
    public static Search(targetNode: Node, name: string): Node {
        if (targetNode.name == name) {
            return targetNode;
        }

        for (let i = 0; i < targetNode.children.length; i++) {
            const child = targetNode.children[i];
            let result = this.Search(child, name);
            if (result != null) {
                return result;
            }
        }

        return null;
    }
}

注:可以获取到多个挂载到此脚本的文本内容

例如:每个框都挂载这个脚本时,通过拖动即可获取到其内容

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Cocos Creator 3.4.2 可以使用 `getBoundingBoxToWorld` 函数来获取一个节点在世界坐标系的包围盒。通过比较两个节点的包围盒是否相交,可以判断它们是否在对方区域内。 以下是一个示例代码: ```typescript // 假设有两个节点 node1 和 node2 const box1 = node1.getBoundingBoxToWorld(); const box2 = node2.getBoundingBoxToWorld(); if (box1.intersects(box2)) { console.log('两个节点相交'); } else { console.log('两个节点不相交'); } ``` 在这个示例,`getBoundingBoxToWorld` 函数会返回一个 `Rect` 对象,表示节点在世界坐标系的包围盒。`intersects` 函数用于判断两个矩形是否相交,如果相交则返回 `true`,否则返回 `false`。 需要注意的是,`getBoundingBoxToWorld` 函数返回的包围盒是一个矩形,它的边缘也包含在内。如果需要排除边缘,可以通过调整包围盒的大小来实现。例如,可以使用 `inset` 函数来缩小包围盒的大小,从而排除边缘的影响: ```typescript const insetSize = 10; // 缩小的大小 const box1 = node1.getBoundingBoxToWorld().inset(insetSize); const box2 = node2.getBoundingBoxToWorld().inset(insetSize); if (box1.intersects(box2)) { console.log('两个节点相交'); } else { console.log('两个节点不相交'); } ``` 在这个示例,`inset` 函数会将包围盒的大小缩小 `insetSize` 像素。这样,如果两个节点的边缘距离小于 `insetSize` 像素,它们仍然不会被判定为相交。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值