用替换或拼接替换阵列中的编号

Replace numbers in Array with replace or splice

本文关键字:替换 编号 拼接 阵列      更新时间:2023-09-26

所以这里的目的是,当球击中数字为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();
    }
}

既然知道命中的行,只需将其替换为每个位置都包含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;

您可以循环浏览该行并将其所有值设置为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;
        });
    }
}