【cocos creator】拖拽排序列表

请添加图片描述

DEMO下载

GameCtrl.ts

import ItemCtrl from "./ItemCtrl";const { ccclass, property } = cc._decorator;@ccclass
export default class GameCtrl extends cc.Component {@property(cc.Node)content: cc.Node = null;@property(cc.Node)prefab: cc.Node = null;arr = []//移动速度moveSpeed = 0.15//排序x间距spacingX = 10nodePool: cc.Node[] = []tempId = 0touchId = nulllastTouchId = nullisAni = falsestart() {//列表数量let count = 8//初始化列表this.isAni = truefor (let i = 0; i < count; i++) {this.scheduleOnce(() => {this.addOne()}, i * 0.1)}this.scheduleOnce(() => {this.isAni = false}, count * 0.1 + this.moveSpeed)}onAddBtnClick() {this.addOne()}onRemoveBtnClick() {if (!this.arr.length) returnlet id = this.lastTouchIdlet index = this.arr.findIndex((value) => { return value.id == id })if (index == -1) id = this.arr[0].idthis.removeOneById(id)this.upDateIndexByX(true)}removeOneById(id) {let index = this.arr.findIndex((value) => { return value.id == id })if (index == -1) returnlet data = this.arr.splice(index, 1)let node: cc.Node = data[0].nodelet toPos = node.positionif (index == 0) toPos = cc.v3(node.position.x - this.prefab.width / 2, node.position.y)cc.tween(node).to(this.moveSpeed, { position: toPos, scale: 1, opacity: 0 }).call(() => {node.stopAllActions()node.active = falsethis.nodePool.push(data[0].node)}).start()}addOne(waitTime = 0) {let node: cc.Node = this.nodePool.shift()if (!node) {node = cc.instantiate(this.prefab)node.parent = this.content}node.opacity = 0;node.scale = 1let pos = this.getItemPos(this.arr.length, this.arr.length + 1)let id = this.tempIdlet data = {name: id,id: id,index: id,node: node,originPos: pos,checkPos: pos}this.arr.push(data);node.getComponent(ItemCtrl).initData(data, this)node.setPosition(pos)node.x = pos.x + this.prefab.widthnode.active = truecc.tween(node).delay(waitTime).call(() => {this.upDateIndexByX(true)}).to(this.moveSpeed, { position: node.position, scale: 1, opacity: 255 }).start()this.tempId++}/*** 获取item排序位置* @param i * @param totalCount * @returns */getItemPos(i, totalCount) {let startX = -(totalCount - 1) * (this.prefab.width + this.spacingX) / 2let pos = cc.v2(0, 0)pos.x = startX + (this.prefab.width + this.spacingX) * ireturn pos}getSidePos() {let totalCount = this.arr.lengthlet startX = -(totalCount - 1) * (this.prefab.width + this.spacingX) / 2let pos = cc.v2(0, 0)let minX = startXlet maxX = startX + (this.prefab.width + this.spacingX) * (totalCount - 1)return { minPos: cc.v2(minX, pos.y), maxPos: cc.v2(maxX, pos.y) }}/*** 按照x轴大小排序* @param isEnd 为true时候强制刷新位置 */upDateIndexByX(isEnd = false) {this.arr.sort(this.sortData)let count = this.arr.length;for (let i = 0; i < count; i++) {let data = this.arr[i]if (!isEnd && data.index == i) continue;data.index = ilet pos = this.getItemPos(i, count)data.originPos = posif (data.node.getComponent(ItemCtrl).isTouch) {continue;}data.checkPos = poscc.tween(data.node).to(this.moveSpeed, { position: pos }).start()}}//获取按照x轴大小sortData(a, b) {return a.checkPos.x - b.checkPos.x}}

ItemCtrl.ts

import GameCtrl from "./GameCtrl";const { ccclass, property } = cc._decorator;@ccclass
export default class ItemCtrl extends cc.Component {@property(cc.Label)desc: cc.Label = null;data: any = {};gameCtrl: GameCtrl = nullprivate _originPos: cc.Vec2;private _startPos: any;private oginPos: any;isTouch = false;start() {this.node.zIndex = 0;this.oginPos = this.node.position;this.regiestNodeEvent(this.node);}/** 节点注册事件 */regiestNodeEvent(node: cc.Node) {if (!node) return;node.on(cc.Node.EventType.TOUCH_START, this.touchStartEvent, this);node.on(cc.Node.EventType.TOUCH_END, this.touchCancel, this);node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchCancel, this);node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveEvent, this);}/*** 传入数据* @param data 数据* @param index 顺序* @param extData 额外数据*/initData(data, gameCtrl) {this.data = data;this.desc.string = data.id + "";this.gameCtrl = gameCtrl}touchStartEvent(event) {if (this.gameCtrl.isAni) returnthis.isTouch = trueconsole.log('touch start--------')this._originPos = this.node.getPosition();this._startPos = event.getLocation();this.node.zIndex = 999;this.node.opacity = 200;this.gameCtrl.getComponent(GameCtrl).touchId = this.data.idthis.gameCtrl.getComponent(GameCtrl).lastTouchId = this.data.id}touchMoveEvent(event) {let pos = event.getLocation();if (!this._startPos) {return;}//控制横轴移动let offset_x = pos.x - this._startPos.x;let toPosX = this._originPos.x + offset_x;let getSidePos = this.gameCtrl.getSidePos()if (toPosX < getSidePos.minPos.x) {toPosX = getSidePos.minPos.x}if (toPosX > getSidePos.maxPos.x) {toPosX = getSidePos.maxPos.x}this.node.x = toPosX//控制纵轴移动// let offset_y = pos.y - this._startPos.y;// this.node.y = this._originPos.y + offset_y;let isRight = this.node.x > this.data.originPos.xlet x = isRight ? (this.node.x + this.node.width / 2) : (this.node.x - this.node.width / 2)//检测重叠超过1/2,判断为移动this.data.checkPos = cc.v2(x, this.data.originPos.y)this.gameCtrl.getComponent(GameCtrl).upDateIndexByX()}touchCancel() {this.isTouch = falsethis.gameCtrl.getComponent(GameCtrl).upDateIndexByX(true)this.node.opacity = 255;this.node.zIndex = 0;this.gameCtrl.getComponent(GameCtrl).touchId = null}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/16251.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Vision Transformer:打破CNN垄断,全局注意力机制重塑计算机视觉范式

目录 引言 一、ViT模型的起源和历史 二、什么是ViT&#xff1f; 图像处理流程 图像切分 展平与线性映射 位置编码 Transformer编码器 分类头&#xff08;Classification Head&#xff09; 自注意力机制 注意力图 三、Coovally AI模型训练与应用平台 四、ViT与图像…

国产编辑器EverEdit - 编辑辅助功能介绍

1 编辑辅助功能 1.1 各编辑辅助选项说明 1.1.1 行号 打开该选项时&#xff0c;在编辑器主窗口左侧显示行号&#xff0c;如下图所示&#xff1a; 1.1.2 文档地图 打开该选项时&#xff0c;在编辑器主窗口右侧靠近垂直滚动条的地方显示代码的缩略图&#xff0c;如下图所示&…

Spring AI 介绍

文章来源&#xff1a;AI 概念 (AI Concepts) _ Spring AI1.0.0-SNAPSHOT中文文档(官方文档中文翻译)|Spring 教程 —— CADN开发者文档中心 本节介绍 Spring AI 使用的核心概念。我们建议仔细阅读它&#xff0c;以了解 Spring AI 是如何实现的。 模型 AI 模型是旨在处理和生成…

Spring MVC 拦截器(Interceptor)与过滤器(Filter)的区别?

1、两者概述 拦截器&#xff08;Interceptor&#xff09;&#xff1a; 只会拦截那些被 Controller 或 RestController 标注的类中的方法处理的请求&#xff0c;也就是那些由 Spring MVC 调度的请求。过滤器&#xff08;Filter&#xff09;&#xff1a; 会拦截所有类型的 HTTP …

qt QCommandLineOption 详解

1、概述 QCommandLineOption类是Qt框架中用于解析命令行参数的类。它提供了一种方便的方式来定义和解析命令行选项&#xff0c;并且可以与QCommandLineParser类一起使用&#xff0c;以便在应用程序中轻松处理命令行参数。通过QCommandLineOption类&#xff0c;开发者可以更便捷…

Flink KafkaConsumer offset是如何提交的

一、fllink 内部配置 client.id.prefix&#xff0c;指定用于 Kafka Consumer 的客户端 ID 前缀partition.discovery.interval.ms&#xff0c;定义 Kafka Source 检查新分区的时间间隔。 请参阅下面的动态分区检查一节register.consumer.metrics 指定是否在 Flink 中注册 Kafka…

从Word里面用VBA调用NVIDIA的免费DeepSeekR1

看上去能用而已。 选中的文字作为输入&#xff0c;运行对应的宏即可&#xff1b;会先MSGBOX提示一下&#xff0c;然后相关内容追加到word文档中。 需要自己注册生成好用的apikey Option ExplicitSub DeepSeek()Dim selectedText As StringDim apiKey As StringDim response A…

网络工程师 (29)CSMA/CD协议

前言 CSMA/CD协议&#xff0c;即载波监听多路访问/碰撞检测&#xff08;Carrier Sense Multiple Access with Collision Detection&#xff09;协议&#xff0c;是一种在计算机网络中&#xff0c;特别是在以太网环境下&#xff0c;用于管理多个设备共享同一物理传输介质的重要…

WPS中如何批量上下居中对齐word表格中的所有文字

大家好&#xff0c;我是小鱼。 在日常制作Word表格时&#xff0c;经常需要对表格中的内容进行排版。经常会把文字设置成左对齐、居中对齐或者是右对齐&#xff0c;这些对齐方式都比较好设置&#xff0c;有时制作的表格需要把文字批量上下居中对齐&#xff0c;轻松几步就可以搞…

GeekPad智慧屏编程控制

前面通过homeassistant和emqx一番折腾&#xff0c;已经可以控制GeekPad智慧屏的开关了。但是这中间用到的软件对环境依赖非常高&#xff0c;想再优化一下&#xff0c;把这两个工具都装到手机上&#xff0c;最后勉强实现了&#xff0c;但是还得借用模拟器和容器&#xff0c;稳定…

【DeepSeek】在本地计算机上部署DeepSeek-R1大模型实战(完整版)

【作者主页】Francek Chen 【专栏介绍】 ⌈ ⌈ ⌈人工智能与大模型应用 ⌋ ⌋ ⌋ 人工智能&#xff08;AI&#xff09;通过算法模拟人类智能&#xff0c;利用机器学习、深度学习等技术驱动医疗、金融等领域的智能化。大模型是千亿参数的深度神经网络&#xff08;如ChatGPT&…

可编程网卡芯片在京东云网络的应用实践【BGW边界网关篇】

目录导览 文章背景 一.网关问题分析 BGW专线网关机器运维变更困难 BGW专线网关故障收敛链路复杂且长 BGW专线网关不具备异构架构下的灾备能力 BGW专线网关硬件资源成本居高不下 二.技术方案设计实现 网络拓扑规划与VIP架构升级 硬件实现与N-Tb流量平滑迁移 三.落地…

接口测试Day12-持续集成、git简介和安装、Gitee远程仓库、jenkins集成

持续集成 概念&#xff1a; 团队成员将自己的工作成果&#xff0c;持续集成到一个公共平台的过程。成员可以每天集成一次&#xff0c;也可以一天集成多 次。 相关工具&#xff1a; 本地代码管理&#xff1a;git远程代码管理&#xff1a;gitee(国内)、github(国外)、gitlib(公司…

前端快速生成接口方法

大家好&#xff0c;我是苏麟&#xff0c;今天聊一下OpenApi。 官网 &#xff1a; umijs/openapi - npm 安装命令 npm i --save-dev umijs/openapi 在根目录&#xff08;项目目录下&#xff09;创建文件 openapi.config.js import { generateService } from umijs/openapi// 自…

三角测量——用相机运动估计特征点的空间位置

引入 使用对极约束估计了相机运动后&#xff0c;接下来利用相机运动估计特征点的空间位置&#xff0c;使用的方法就是三角测量。 三角测量 和对极几何中的对极几何约束描述类似&#xff1a; z 2 x 2 R ( z 1 x 1 ) t z_2x_2R(z_1x_1)t z2​x2​R(z1​x1​)t 经过对极约束…

WPS计算机二级•文档的文本样式与编号

听说这是目录哦 标题级别❤️新建文本样式 快速套用格式&#x1fa77;设置标题样式 自定义设置多级编号&#x1f9e1;使用自动编号&#x1f49b;取消自动编号&#x1f49a;设置 页面边框&#x1f499;添加水印&#x1fa75;排版技巧怎么分栏&#x1f49c;添加空白下划线&#x…

【编程实践】vscode+pyside6环境部署

1 PySide6简介 PySide6是Qt for Python的官方版本&#xff0c;支持Qt6&#xff0c;提供Python访问Qt框架的接口。优点包括官方支持、LGPL许可&#xff0c;便于商业应用&#xff0c;与Qt6同步更新&#xff0c;支持最新特性。缺点是相比PyQt5&#xff0c;社区资源较少。未来发展…

soular基础教程-使用指南

soular是TikLab DevOps工具链的统一帐号中心&#xff0c;今天来介绍如何使用 soular 配置你的组织、工作台&#xff0c;快速入门上手。 &#xfeff; 1. 账号管理 可以对账号信息进行多方面管理&#xff0c;包括分配不同的部门、用户组等&#xff0c;从而确保账号权限和职责…

访问Elasticsearch服务 curl ip 端口可以 浏览器不可以

LINUX学习 在虚拟机上面的linux上面用docker 部署Elasticsearch项目后&#xff0c;在linux系统内部用curl ip 端口地址的形式可以访问到Elasticsearch。可以返回数据。 但是在本机的浏览器中输入ip 端口&#xff0c;会报错&#xff0c;找不到服务。 ping 和 trelnet均不通。 …

防火墙安全综合实验

防火墙安全综合实验 一、拓扑信息 二、需求及配置 实验步骤 需求一&#xff1a;根据下表&#xff0c;完成相关配置 设备接口VLAN接口类型SW2GE0/0/2VLAN 10AccessGE0/0/3VLAN 20AccessGE0/0/1VLAN List&#xff1a;10 20Trunk 1、创建vlan10和vlan20 2、将接口划分到对应…