The strategy for using Minecraft blocks as buttons to control a program is:
- Place the buttons on the screen in a place convenient to the player.
- In the block on-broken event, use the code that would have been in a on-chat command to do a sequence of actions,
- Replace the broken block with another of the same kind.
We modified the existing program described in Handy Timer — Block-by-Block Tutorial Video in MakeCode for Minecraft , to illustrate using Minecraft blocks as buttons. Only the changed code is described here. This code assumes a flat world such as that described in MakeCode for Minecraft Sandbox World: Make It Flat and Simple.
The placeButtons Function
The placeButtons function does exactly what its name implies: it places each button that will be used in lieu of an on-chat command. I like to place the buttons in front of the player within easy reach so that the player need not move to break a block (click a button). In this case we placed red concrete to do the same thing as the stop on-chat command, yellow for the reset command and green for the go command.
Call the placeButtons Function
Once the function is built, it must be called from every code that initializes a screen. Here it is called from on start and from moveMe. As explained in the next section, it is also called from each block’s on-broken event.
Code the on-broken Events
Each block being used as a button must have an on-broken event in which the same code as in the corresponding on-chat command should be run. I like to move the code to a function, which is called from both the on-broken event and the on-chat command. The advantage of putting the code in a function is that if it is modified, it need be changed in only one place.
Each on-broken event must also call placeButtons. Of course, an on-broken event really must replace only the broken button, but the advantage of calling the placeButtons event is that if you want to futz with the location of the buttons, it need be changed only in one place — always a good thing.
Get the Code
Handy Timer with Blocks for Buttons code is shared on Code Connection at this URL
To get and use the code, follow these steps.
Click the Import button , which is upper right in the Code Connection window just below the banner. This will open the window shown below.
Click the Import URL button , which is on the right, to open the window shown below.
Paste the URL supplied for the program you want to use in the space under the text “Copy the URL …”
Click the Go ahead! button .
The next window you will see will be the MakeCode window with the code downloaded from the URL. At this point, you can treat it like any other code, e.g., run it, save it locally, modify it, publish your changes or whatever else your heat desires.
We have tested several other methods of downloading the code using the URL, for example, pasting the URL in a browser. No joy. For more detailed instruction see our post How to Use Shared MakeCode on Microsoft Code Connection for Minecraft.