当前位置: 首页 > news >正文

【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.lryc.cn/news/535184.html

相关文章:

  • b站——《【强化学习】一小时完全入门》学习笔记及代码(1-3 多臂老虎机)
  • 【Mac排错】ls: command not found 终端命令失效的解决办法
  • 探秘Hugging Face与DeepSeek:AI开源世界的闪耀双子星
  • SkyWalking 10.1.0 实战:从零构建全链路监控,解锁微服务性能优化新境界
  • 本地部署DeepSeek-R1(Mac版)
  • 网易易盾接入DeepSeek,数字内容安全“智”理能力全面升级
  • apachePoi中XSSFClientAnchor图片坐标简述;填充多张图片
  • Java、Go、Rust、Node.js 的内存占比及优缺点分析
  • C++智能指针的使用
  • 计算机毕业设计——Springboot的社区维修平台旅游管理
  • MySQL ALTER 命令详解
  • 02、QLExpress从入门到放弃,相关API和文档
  • Mp4视频播放机无法播放视频-批量修改视频分辨率(帧宽、帧高)
  • deepseek大模型集成到idea
  • AI基础 -- AI学习路径图
  • 在 Visual Studio Code 与微信开发者工具中调试使用 emscripten 基于 C 生成的 WASM 代码
  • elasticsearch实战应用从入门到高效使用java集成es快速上手
  • 【OneAPI】通过网页预渲染让搜索引擎收录网页
  • 【网络安全.渗透测试】Cobalt strike(CS)工具使用说明
  • 港中文腾讯提出可穿戴3D资产生成方法BAG,可自动生成服装和配饰等3D资产如,并适应特定的人体模型。
  • 【C语言标准库函数】标准输入输出函数详解[4]:二进制文件读写函数
  • Python:凯撒密码
  • C++引用深度详解
  • C++ Primer 语句作用域
  • github - 使用
  • 内网ip网段记录
  • k8s部署logstash
  • EF Core中实现值对象
  • 【分布式理论9】分布式协同:分布式系统进程互斥与互斥算法
  • 木材表面缺陷检测数据集,支持YOLO+COCO JSON+PASICAL VOC XML+DARKNET格式标注信息,平均正确识别率95.0%