我正在制作一個部分是俄羅斯方塊克隆的游戲,但是一旦它們充滿方塊,我就無法讓線條消失。該游戲目前有 11 行可以填充,每行由在 create 方法中創建的精靈陣串列示。
this.row1 = [null, null, null, null, null, null, null, null, null, null];
this.row2 = [null, null, null, null, null, null, null, null, null, null];
this.row3 = [null, null, null, null, null, null, null, null, null, null];
this.row4 = [null, null, null, null, null, null, null, null, null, null];
this.row5 = [null, null, null, null, null, null, null, null, null, null];
this.row6 = [null, null, null, null, null, null, null, null, null, null];
this.row7 = [null, null, null, null, null, null, null, null, null, null];
this.row8 = [null, null, null, null, null, null, null, null, null, null];
this.row9 = [null, null, null, null, null, null, null, null, null, null];
this.row10 = [null, null, null, null, null, null, null, null, null, null];
this.row11 = [null, null, null, null, null, null, null, null, null, null];
我只是想在處理其他行之前先處理最后一行 row1。當塊在更新方法中使用此代碼登陸時,我填充了陣列:
if (this.activeBlock.y == 672){
if(this.activeBlock.x == 352){
this.row1[0] = this.activeBlock;
}
else if(this.activeBlock.x == 416){
this.row1[1] = this.activeBlock;
}
else if(this.activeBlock.x == 480){
this.row1[2] = this.activeBlock;
}
else if(this.activeBlock.x == 544){
this.row1[3] = this.activeBlock;
}
else if(this.activeBlock.x == 608){
this.row1[4] = this.activeBlock;
}
else if(this.activeBlock.x == 672){
this.row1[5] = this.activeBlock;
}
else if(this.activeBlock.x == 736){
this.row1[6] = this.activeBlock;
}
else if(this.activeBlock.x == 800){
this.row1[7] = this.activeBlock;
}
else if(this.activeBlock.x == 864){
this.row1[8] = this.activeBlock;
}
else if(this.activeBlock.x == 928){
this.row1[9] = this.activeBlock;
}
當底行已滿時,將觸發此代碼以清除它并將上面的所有內容向下移動:
if (this.isFull(this.row1)){
this.clearRow(this.row1);
this.moveDown(this.row1, this.row2);
this.moveDown(this.row2, this.row3);
this.moveDown(this.row3, this.row4);
this.moveDown(this.row4, this.row5);
this.moveDown(this.row5, this.row6);
this.moveDown(this.row6, this.row7);
this.moveDown(this.row7, this.row8);
this.moveDown(this.row8, this.row9);
this.moveDown(this.row9, this.row10);
this.moveDown(this.row10, this.row11);
}
這些是我用來執行最后一部分的自定義方法:
isFull(row){
this.result = true;
this.i = 0;
while(this.i < row.length){
if (row[this.i] == null){
this.result = false;
}
this.i = this.i 1;
}
return this.result;
}
clearRow(row){
this.i = 0;
while(this.i < row.length){
row[this.i].destroy();
row[this.i] = null;
}
}
moveDown(bottRow, topRow){
this.i = 0;
while(this.i < bottRow.length()){
topRow[this.i] = topRow[this.i].y 64;
bottRow[this.i] = topRow[this.i];
topRow[this.i] = null;
this.i = this.i 1;
}
}
在大多數情況下,代碼似乎可以正常作業,但是當底行已滿時游戲會凍結,并且在檢查檢查螢屏是否有錯誤時,它指向 clearRow(row) 方法中的 destroy() 。錯誤訊息如下:無法讀取 null 的屬性(讀取“銷毀”)
我不確定出了什么問題。誰能告訴?
如果有幫助,我將在 VSCode 中使用街機物理學中的 Phaser 3。
uj5u.com熱心網友回復:
您永遠不會在回圈中遞增 i,因此在將 row[0] 設定為 null 之后,您將在下次回圈中再次嘗試訪問它。使 clearRow 更像您的 isFull 函式。所以是:
clearRow(row){
this.i = 0;
while(this.i < row.length){
row[this.i].destroy();
row[this.i] = null;
this.i = this.i 1; // Add this line
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/535550.html
