提示コードのconsole.log(this.comboArray.length);
部ですが再帰的処理で4っブロックが連続して並んでいるかという処理を記述しているんのですが.push
してもコンソールログで要素数を見ると全て1
になってしまいます。これは何が原因なのでしょか?this.combo
はグローバル変数です。
ComboCheck(pos,puyoColor,vec) { if(pos.y < STAGE_WIDTH - 1) { if( (board[pos.y][pos.x + 1].toString() == puyoColor.toString()) && ((vec.y != pos.y) && (vec.x != pos.x + 1)) ) { this.combo++; this.comboArray.push(pos); console.log(this.comboArray.length); if(this.comboArray.length == 4 ) { console.log("444"); return this.comboArray; } else { this.p = new Vector(0,0); this.p.x = pos.x + 1; this.p.y = posy; vec.x = pos.x + 1; vec.y = pos.y; return this.ComboCheck(this.p,puyoColor,vec); } } } if(pos.x > 0) { if( (board[pos.y][pos.x - 1].toString() == puyoColor.toString()) && ((vec.y != pos.y) && (vec.x != pos.x - 1)) ) { this.combo++; this.comboArray.push(pos); console.log(this.comboArray.length); if(this.comboArray.length == 4 ) { console.log("444"); return this.comboArray; } else { this.p = new Vector(0,0); this.p.x = pos.x - 1; this.p.y = pos.y; vec.x = pos.x - 1; vec.y = pos.y; return this.ComboCheck(this.p,puyoColor,vec); } } } if(pos.y > 0) { if( (board[pos.y - 1][pos.x].toString() == puyoColor.toString()) && (( (vec.y - 1) != pos.y) && (vec.x != pos.x)) ) { this.combo++; this.comboArray.push(pos); console.log(this.comboArray.length); if(this.comboArray.length == 4) { console.log("444"); return this.comboArray; } else { this.p = new Vector(0,0); this.p.x = pos.x; this.p.y = pos.y - 1; vec.x = pos.x; vec.y = pos.y - 1; return this.ComboCheck(this.p,puyoColor,vec); } } } if(pos.y < STAGE_HEIGHT - 1) { if( (board[pos.y + 1][pos.x].toString() == puyoColor.toString()) && ((vec.y + 1 != pos.y) && (vec.x != pos.x)) ) { this.comboArray.push(pos); console.log(this.comboArray.length); if(this.comboArray.length == 4) { console.log("444"); return this.comboArray; } else { this.p = new Vector(0,0); this.p.x = pos.x; this.p.y = pos.y + 1; vec.x = pos.x; vec.y = pos.y + 1; return this.ComboCheck(this.p,puyoColor,vec); } } } return this.comboArray; } Clear() { for(let y = 0; y < STAGE_HEIGHT; y++) { for(let x = 0; x < STAGE_WIDTH; x++) { if( (board[y][x] != WALL) && (board[y][x] != None) ) { this.comboArray = new Array(); this.vec = new Vector(0,0); this.combo = 0; this.ca = this.ComboCheck(new Vector(x,y),board[y][x],this.vec); console.log("this.ca.length " + this.ca.length); if(this.ca.length == 4) { console.log("コンボ !"); this.comboArray,length = 0; } } } } }
デバッグ丸投げなので低評価しました。
どうしても理由がわからなくてデバッグ丸投げになってしまうこともままあるとは思いますが、そういうときは、
・誰でもすぐに問題を再現できるように、完全なコードを開示する
・期待する動作を書く
は最低限やってください。