【cocos creator】拖拽排序列表

embedded/2025/2/11 15:06:12/

请添加图片描述

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.ppmy.cn/embedded/161352.html

相关文章

Linux 安装 Ollama

1、下载地址 Download Ollama on Linux 2、有网络直接执行 curl -fsSL https://ollama.com/install.sh | sh 命令 3、下载慢的解决方法 1、curl -fsSL https://ollama.com/install.sh -o ollama_install.sh 2、sed -i s|https://ollama.com/download/ollama-linux|https://…

高效利用Python爬虫获取淘宝店铺详情:电商数据挖掘

在电商行业竞争日益激烈的当下&#xff0c;精准且高效地获取淘宝店铺详情对于商家和数据分析师来说至关重要。无论是进行市场调研、优化商品布局&#xff0c;还是制定竞争策略&#xff0c;店铺详情数据的全面掌握都是关键。Python爬虫技术以其强大的功能和灵活性&#xff0c;成…

DeepSeek之于心理学的一点思考

模型和硬件参数对应关系参考 模型参数规模 典型用途 CPU建议 GPU建议 最小内存建议 磁盘空间建议 适用场景 1.5b(15亿) 小型推理、轻量级任务 4核以上(Intel i5/AMD Ryzen5) 可选&#xff0c;入门级GPU(如NVIDIA GTX1650 4GB显存) 8GB 10GB以上SSD 小型NLP任务、文…

Python Pandas(7):Pandas 数据清洗

数据清洗是对一些没有用的数据进行处理的过程。很多数据集存在数据缺失、数据格式错误、错误数据或重复数据的情况&#xff0c;如果要使数据分析更加准确&#xff0c;就需要对这些没有用的数据进行处理。数据清洗与预处理的常见步骤&#xff1a; 缺失值处理&#xff1a;识别并…

AF3 ExponentialMovingAverage类解读

AlphaFold3 的 ExponentialMovingAverage (EMA) 类,用于维护神经网络模型参数的指数加权移动平均。它可以在训练过程中对模型的参数进行平滑处理,以减缓参数更新的波动,帮助提升模型的泛化能力。 主要功能 EMA 通过对每个参数的移动平均来稳定模型的训练过程。在每一步,参…

ctfshow-36D杯

ctfshow-36D杯 给你shell ($obj[secret] ! $flag_md5 ) ? haveFun($flag) : echo "here is your webshell: $shell_path"; 这是个弱比较&#xff0c;输入?give_me_shell前三个是0说明二进制小于1000000就是ASCII的64&#xff0c; 0-32是不可见或非打印字符&…

日志2025.2.9

日志2025.2.9 1.增加了敌人挥砍类型 2.增加了敌人的死亡状态 在敌人身上添加Ragdoll&#xff0c;死后激活布偶模式 public class EnemyRagdoll : MonoBehaviour { private Rigidbody[] rigidbodies; private Collider[] colliders; private void Awake() { rigidbodi…

C#Halcon窗体鼠标交互生成菜单

窗体鼠标交互生成菜单&#xff0c;移动鼠标作出相应的提示&#xff0c;并且可以进入相应事件。&#xff08;一般可以应用到成品效果展示&#xff0c;或实战项目检测失败时&#xff0c;需做出人机交互选择时可应用&#xff0c;相对于按键交互&#xff0c;可以优化UI布局&#xf…