[英]Replace numbers in Array with replace or splice
So the purpose here is, when the ball hits the brick with number 6 it has to replace the whole row with number 0. When I use splice for example: stenen.splice(i--,1) it deletes the row without leaving any whitespace but with that number 0 it it possible to create whitespace as you can see in the code, but I don't know how?所以这里的目的是,当球击中编号为 6 的砖时,它必须用编号 0 替换整行。例如,当我使用 splice 时:stenen.splice(i--,1) 它删除该行而不留下任何空格,但使用该数字 0 可以创建空格,如您在代码中所见,但我不知道如何创建?
var stenenPerRij = 27;
var steenHoogte = 20;
var steenBreedte = canvas.width/stenenPerRij;
var stenen = [
[0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,1,1,5,5,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,1,5,5,5,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,1,1,5,5,5,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,1,1,5,5,5,5,1,1,1,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,1,1,5,5,5,5,5,5,1,1,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,1,5,5,5,5,5,5,5,1,1,0,0,0,0,0,0,0,0],
[0,0,0,1,1,1,1,1,1,1,5,5,5,5,5,5,5,5,1,1,0,0,0,0,0,0,0],
[0,1,1,1,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,1,1,1,1,1,1,0,0],
[1,1,5,5,5,5,1,1,1,1,5,5,5,5,5,5,5,5,5,5,5,5,1,1,1,0,0],
[1,5,5,5,1,1,1,5,5,1,1,1,5,5,5,5,5,5,5,5,5,5,5,5,1,1,0],
[1,1,5,5,5,5,5,5,5,5,1,1,1,5,5,5,5,5,5,5,5,5,5,5,5,1,1],
[0,1,1,5,5,5,5,1,1,1,1,1,1,5,5,5,5,5,5,5,5,5,5,5,5,1,1],
[0,1,1,1,1,1,1,1,5,5,5,5,1,1,5,5,5,5,5,5,5,5,5,5,5,1,1],
[0,1,1,5,5,5,5,5,5,5,1,1,1,1,5,5,5,5,5,5,5,5,5,5,5,1,1],
[0,0,1,1,1,1,1,1,1,1,1,5,5,1,1,5,5,5,5,5,5,5,5,5,1,1,0],
[0,0,0,1,5,5,5,5,5,5,5,5,1,1,1,1,5,5,5,5,5,1,1,1,1,0,0],
[0,0,0,1,1,5,5,5,5,1,1,1,1,1,1,5,5,5,5,1,1,1,1,1,1,0,0],
[0,0,0,0,1,1,1,1,1,1,5,5,5,5,1,1,5,5,1,1,0,0,0,0,0,0,0],
[0,0,0,0,0,0,1,5,5,5,5,5,1,1,1,5,1,1,1,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0]
];
function makenMuur() {
for(var i = 0; i < stenen.length; i = i+1) {
for(var j = 0; j < stenen[i].length; j = j+1) {
tekenenStenen(j,i,stenen[i][j]);
}
}
}
function tekenenStenen(x,y,stenen) {
switch(stenen) {
case 1:
mijnObject.fillStyle = "#0d0d0d";
break;
case 2:
mijnObject.fillStyle = "#333333";
break;
case 3:
mijnObject.fillStyle = "#595959";
break;
case 4:
mijnObject.fillStyle = "#808080";
break;
case 5:
mijnObject.fillStyle = "#a6a6a6";
break;
default:
mijnObject.clearRect(0, 0, steenBreedte, steenHoogte);
break;
}
if(stenen) {
mijnObject.beginPath();
mijnObject.strokeStyle = "#000000";
mijnObject.rect(x*steenBreedte, y*steenHoogte, steenBreedte, steenHoogte);
mijnObject.fill();
mijnObject.stroke();
mijnObject.closePath();
}
}
You can loop through that row and set all its values to 0:您可以遍历该行并将其所有值设置为 0:
var rowIndex = 6;
var myRow = allMyRows[rowIndex];
for (var i = 0; i < myRow.length; i++) {
x[i] = 0;
}
function changeWholeRowToZero(x){ // x is the row index, start from 0. if (typeof stenen[x] != 'undefined'){ $.each(stenen[x], function(key, val){ stenen[x][key] = 0; }); } }
Since you know the row that's hit, just replace it with an array containing 0 in each position, eg由于您知道命中的行,只需将其替换为每个位置都包含 0 的数组,例如
var nullRij = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
// other code
stenen[indexOfHitRow] = nullRij;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.