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


    1. import { _decorator, Component, Event, EventTouch, find, Input, input, Label, Node, Rect, sys, TextAsset, tween, UIOpacity, UITransform, Vec2, Vec3 } from 'cc';
    2. import { JsonMgr } from '../Managers/JsonMgr';
    3. import { strings } from '../Struct/GameStruct';
    4. import { GAMEFUNC } from '../Tip/GameFunc';
    5. import { SingleMgr } from '../Managers/SingleMgr';
    6. const { ccclass, property } = _decorator;
    7. ///用于获取手指滑动区域的文本并记录文本内容
    8. @ccclass('Getstring')
    9. export class Getstring extends Component {
    10. label: Label;
    11. b: boolean = true;
    12. op:UIOpacity;
    13. onLoad() {
    14. let a = GAMEFUNC.Search(this.node, "name");
    15. this.label = a.getComponent(Label);
    16. this.op=a.getComponent(UIOpacity);
    17. //this.label = this.node.getChildByName("name").getComponent(Label);
    18. //input.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);
    19. //input.on(Input.EventType.TOUCH_START, this.onTouchStart, this);
    20. input.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
    21. this.node.parent.parent.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
    22. //this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
    23. //JsonMgr.getInstance().SetJsonOne(strings.InputTxt,""); //初始化数据
    24. }
    25. start() {
    26. }
    27. update(deltaTime: number) {
    28. }
    29. onTouchEnd(event: EventTouch) {
    30. this.b = true;
    31. //JsonMgr.getInstance().SetJsonOne(strings.InputTxt,""); //清空数据
    32. //判断内容
    33. //JsonMgr.getInstance().SetJson(strings.InputTxt);
    34. SingleMgr.getInstance().csvKnow = true; //允许查看
    35. // if(SingleMgr.getInstance().ShowText){
    36. // console.log("渐变!!!");
    37. // tween(this.op)
    38. // .to(1, {opacity: 0})
    39. // .to(1, {opacity: 255})
    40. // .start();
    41. // SingleMgr.getInstance().ShowText=false;
    42. // }
    43. }
    44. onTouchMove(event: EventTouch) {
    45. const x=event.touch.getUILocationX();
    46. const y=event.touch.getUILocationY();
    47. //const touchPos = event.getLocation();
    48. //const localPos = this.node.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(touchPos.x, touchPos.y));
    49. const localPos = this.node.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(x, y));
    50. const size = this.node.getComponent(UITransform).contentSize;
    51. const rect = new Rect(-size.width / 2, -size.height / 2, size.width, size.height);
    52. //console.log("位置更新:"+localPos);
    53. if (rect.contains(new Vec2(localPos.x, localPos.y))) {
    54. const text = this.label.string;
    55. if (this.b) {
    56. //动画效果
    57. tween(this.node)
    58. .to(0.2, { scale: new Vec3(1.2, 1.2, 1.2) })
    59. .to(0.1, { scale: new Vec3(1, 1, 1) })
    60. .start();
    61. let inputText = JsonMgr.getInstance().GetJson(strings.InputTxt);
    62. // 在这里操作获取到的文本内容
    63. JsonMgr.getInstance().SetJsonOne(strings.InputTxt, inputText + text);//字符串连接
    64. this.b = false;
    65. let te = JsonMgr.getInstance().GetJson(strings.InputTxt); //字符串赋值
    66. console.log(te);
    67. }
    68. }
    69. }
    70. }
    71. //递归获取子物体
    72. public static Search(targetNode: Node, name: string): Node {
    73. if (targetNode.name == name) {
    74. return targetNode;
    75. }
    76. for (let i = 0; i < targetNode.children.length; i++) {
    77. const child = targetNode.children[i];
    78. let result = this.Search(child, name);
    79. if (result != null) {
    80. return result;
    81. }
    82. }
    83. return null;
    84. }
    85. }

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

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

  • 相关阅读:
    细数35个单元测试准则
    两数乘积:输出1~100整数乱序列表中两数乘积是目标整数的最小下标对
    PCIe实用调试工具MindShare Arbor增加试用天数
    file.close总是标红的解决方法
    SpringCloud入门学习
    一篇带你学完HTML基础
    基于springcloud+web实现智慧养老平台系统项目【项目源码+论文说明】计算机毕业设计
    修改Jenkins主目录
    浅谈电力电容器技术的发展及选型
    C++文件操作->文本文件(->写文件、读文件)、二进制文件(->写文件、读文件)
  • 原文地址:https://blog.csdn.net/m0_71624363/article/details/134262646