简体   繁体   中英

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?

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:

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM