记录一个小算法 scroll 上指定的一个ui 元素 移动到屏幕中心。 考虑元素位于边界的问题
引擎是白鹭,坐标系 和 cocos_creator 不同
this.max_right = GameConfig.curWidth() - this.mainGroup.width;//this.mainGroup.width 内容的宽
this.max_bottom = GameConfig.curHeight() - this.mainGroup.height;//this.mainGroup.height内容的高
//移动到屏幕中心点 考虑边界的情况 //x: -5629 y: -2076 位置定位 if(gridItem.x<=GameConfig.curWidth()/2){//X左边界 this.mainGroup.x = 0; }else if(gridItem.x>-this.max_right + GameConfig.curWidth()/2 ){//Y右边界 this.mainGroup.x = this.max_right; }else{ this.mainGroup.x = -gridItem.x + GameConfig.curWidth()/2; } if(gridItem.y<=GameConfig.curHeight()/2){//y顶边界 this.mainGroup.y = 0; }else if(gridItem.y>-this.max_bottom + GameConfig.curHeight()/2 ){//y底边界 this.mainGroup.y = this.max_bottom; }else{ this.mainGroup.y = -gridItem.y + GameConfig.curHeight()/2; }
不考虑边界的算法:
this.mainGroup.x = -gridItem.x + GameConfig.curWidth()/2;
this.mainGroup.y = -gridItem.y + GameConfig.curHeight()/2;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | Math.min 和 Math.max 方案: /** 全部通关居中展示 有通关的定位到通关的位置 0全部通关,不为0定位到cityId的位置*/ updateMapPoint(cityPosId: number) { let group = this [`cityGroup_${cityPosId}`] as eui.Group; let midStageWidth = Tools.curWidth() / 2; let midStageHeight = Tools.curHeight() / 2; let x = 0; let y = 0; x = midStageWidth - group .x; y = midStageHeight - group .y; x = Math.min(x, 0); y = Math.min(y, 50); //顶部面板的高度 this .x = Math.max(x, -( this .width - Tools.curWidth())); this .y = Math.max(y, -( this .height - Tools.curHeight() + 100)); //加下边操作按钮的高度 // egret.Tween.get(this).to({x : x,y: y},300); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南