使用replace或splice替换Array中的数字

时间:2016-05-07 12:00:15

标签: javascript arrays

所以这里的目的是,当球击中数字6的砖时,它必须用数字0替换整行。当我使用拼接例如: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();
    }
}

3 个答案:

答案 0 :(得分:0)

您可以遍历该行并将其所有值设置为0:

var rowIndex = 6;
var myRow = allMyRows[rowIndex];

for (var i = 0; i < myRow.length; i++) {
    x[i] = 0;
}

答案 1 :(得分: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;
        });
    }
}

答案 2 :(得分:0)

由于你知道所击中的行,只需将其替换为每个位置包含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;