1. 程式人生 > 實用技巧 >記錄一個小演算法 scroll 上指定的一個ui 元素 移動到螢幕中心。 考慮元素位於邊界的問題

記錄一個小演算法 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;