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

TypeScript 复制代码
import { _decorator, Component, Event, EventTouch, find, Input, input, Label, Node, Rect, sys, UITransform, Vec2, Vec3 } from 'cc';
import { JsonMgr } from '../Managers/JsonMgr';
import { strings } from '../Struct/GameStruct';
const { ccclass, property } = _decorator;

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

    b:boolean=true;
    onLoad() {
        //name为需要找到的物体名称
        let a=Getstring.Search(this.node,"name");
        this.label=a.getComponent(Label);
        //this.label = this.node.getChildByName("name").getComponent(Label);
        input.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);
        input.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
        //this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this)
        JsonMgr.getInstance().SetJsonOne(strings.InputTxt,"");  //初始化数据
    }
    start() {

    }

    update(deltaTime: number) {
        //input.on(Input.EventType.TOUCH_START, this.onTouchEnd, this);
    }


    onTouchEnd(event: EventTouch) {     
        this.b=true;      
        //JsonMgr.getInstance().SetJsonOne(strings.InputTxt,"");    //清空数据
        //判断内容
    }

    onTouchMove(event: EventTouch) {
        const touchPos = event.getLocation();
        const localPos = this.node.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(touchPos.x, touchPos.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) {
                // 在这里操作获取到的文本内容                
                JsonMgr.getInstance().SetJsonOne(strings.InputTxt,sys.localStorage.getItem(strings.InputTxt) + text);//字符串连接
                this.b=false;
                let te=sys.localStorage.getItem(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;
    }
}

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

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

相关推荐
叫我阿柒啊8 小时前
从Java全栈到前端框架:一次真实面试的深度复盘
java·spring boot·typescript·vue·database·testing·microservices
烛阴10 小时前
【TS 设计模式完全指南】用工厂方法模式打造你的“对象生产线”
javascript·设计模式·typescript
定栓10 小时前
Typescript入门-类型断言讲解
前端·javascript·typescript
Thetimezipsby14 小时前
基于Taro4打造的一款最新版微信小程序、H5的多端开发简单模板
前端·javascript·微信小程序·typescript·html5·taro
流影ng15 小时前
【TypeScript】闭包
typescript
葡萄城技术团队16 小时前
TypeScript 队列实战:从零实现简单、循环、双端、优先队列,附完整测试代码
typescript
极客柒20 小时前
Unity 塔防自用可视化路点寻路编辑器
unity·编辑器·游戏引擎
一线灵1 天前
跨平台游戏引擎 Axmol-2.8.1 发布
junit·游戏引擎
烛阴1 天前
【TS 设计模式完全指南】从“入门”到“劝退”,彻底搞懂单例模式
javascript·设计模式·typescript
lypzcgf1 天前
Coze源码分析-资源库-删除插件-前端源码-核心组件实现
前端·typescript·前端框架·react·coze·coze插件·智能体平台