记录一个小算法 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;


  
      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);
        }

  

 

 

原文地址:https://www.cnblogs.com/porter/p/14301695.html