Advance-topic: 'air' block

You may have noticed that there's no function that lets you delete a block from the game world. In CodeCraft, there is a special material value 0. When you call setBlock() with 0 as material, it removes the block if there is one. This special material is not in the materials object. But we can add a new key:value pair using value assignment:

object[key] = value

We can insert a material to represent empty block with value 0. Let's name it 'air'.

// add new key:value pair to materials object

materials["air"] = 0 
console.log(materials)

Console result:

{"box_black": 1, "box_blue": 2,.... "tnt": 88, "air": 0}

Since the air block indicates an invisible space, we can test it by making an existing block disappear when the air block replaces it. First we build a brick column, then build another air block in the middle of the column.

Test the "air" material

// put up a brick column
column(0, -10, 10, 'brick');

// make the middle block disappear
block(0, 5, -10, 'air');

// build a cobblestone column, and make every other block disappear
column(3, -10, 10, 'cobblestone');

for (let j = 1; j < 10; j += 2) {
    block(3, j, -10, 'air');
}

See the picture at the end of the lesson.

Function dig(x, y, z)

Many of you have wanted to be able to click and dig holes in the game world. While we cannot click and dig yet (this will be covered later when we talk about events and callback functions), we can now define a function to "dig" holes in the ground or an existing structure:

function dig(x, y, z) {
    block(x, y, z, 'air');
}

Let's call dig() at some locations on a wall. Check out the picture at the bottom of this page.

Holes in the wall
// build a blue wall using columns
for (let i = 5; i < 10; i++) {
    column(i, -10, 10, 'linen_blue');
}

// make a few holes
dig(5, 5, -10);
dig(6, 8, -10);
dig(8, 5, -10);
dig(9, 9, -10);

Say cheese! It's a blue cheese.

results matching ""

    No results matching ""