egret之一維,二維數組


一維數組轉換成二維數組下標公式:

行=下標/二維數組列數

列=下標%二維數組列數+1

this.row1 = Math.floor(index1 / this.col[this.lev - 1] + 1);
this.col1 = Math.floor(index1 % this.col[this.lev - 1] + 1);

二維數組轉一維數組:

下標=(二維數當前行-1)*列數+二維數當前列-1;

let index1 = (this.row1 - 1) * this.col[this.lev - 1] + (this.col1 - 1);

  眾所周知,js,ts沒有二維數組,但是我們往往需要使用二維數組,上次做連連看直接用一維數組差點暈哭博主,加上馬上又要做一款消消樂,博主決定深入研究一下,實現二維數組。大家都是知道可以通過使用一維數組,達到二維數組的效果,下面直接代碼奉上:

let arr = new Array();
        for (let i = 0; i < 10; i++) {
            arr[i] = new Array();
            for (let j = 0; j < 10; j++) {
                let button = new eui.Button();
                button.x = 100 * j;
                button.y = 100 * i;
                this.addChild(button);
                arr[i][j] = button;
            }
        }
        arr[5][5].addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
            egret.log("hahah1");
        }, this);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM