đ˛ â Create a Dice App and Roll Away!
Love playing board games?
Itâs Saturday night, and your family gathers around to play your favorite board game. You open the box, andâŚOh no! You realize a die is missing. Donât worry, Sketchware has your back. Today, we will create a simple Dice application that will always stay in your phone!
What youâll need
- Sketchware Application from Google Play
What youâll learn
- Concept of Random, Image, and MoreBlock
Step 1: Starting the project
Step 2: Designing the Application
This is going to be a single-paged application â which means the application wonât have any other views. The design above doesnât seem too difficult; if you donât think you can design this, please review previous tutorials.
Step 3: Understanding MoreBlock
MoreBlocks!
Before we move on, I want to talk more about MoreBlock. What is a MoreBlock? Basically, MoreBlock is a customizable function block you can create on your own. If you want to create a block that adds String and number, for example:
âHello Worldâ + 3 = âHello World3â
Sure, you can do this every time you need to add a String and a number; however, what if you were to use this same operation for five times⌠or maybe five hundred times?
It would not be the most efficient way to use this method over and over. Imagine â if the operation was more complicated, the logic section would look very messy.
This is where MoreBlock comes in. You create a custom block that adds a String and a number, and you can use that block over and over.
MoreBlock handles the repetitive logic for you.
Creating a MoreBlock
Under the blocks, go to the More Block tab and click on Make block.
You will be prompted with this pop-up.
There are a couple things you need to know:
1. Name of the block you want to create, for this example, we can call it StringAddNumber
2. Boolean, Number, String blocks it will take as an input. For this block, we want it to take String and Number.
3. Label for any extra description; for this example, we can add a + label between two inputs.
After we create the block, it should look something like this:
Does this block work out of the box? We wish it did â now we need to create logic for this block to operate on.
Creating Logic for MoreBlock
If you check the Logic tab, you will see that a new event is created. Click on the event.
This is where we edit the logic for the block. Note that the inputs weâve defined are already given to us â we only need to define how these values are going to be manipulated.
Just like how we previously added the strings, we add the blocks we need like this:
Note that showMessage block was used just for connecting purposes. Now, we can simplify what looks like this to this!
Remember!
Remember that MoreBlocks donât need to take in input. If you want to make a MoreBlock that performs a certain action, you can do that as well!
Now that we understood what MoreBlock does, letâs apply this idea into our Dice app. Please note that we are NOT going to use this âStringAddNumberâ MoreBlock inside our Dice Application.
Step 4: Setting up the logic
Letâs look at the demo before we think about how the application should function. From the observation, we can conclude:
1. The user clicks on the âDRAW DICEâ button.
2. The application creates a random number between 1 and 6.
3. Depending on the number, we display the image accordingly.
Weâre going to create a MoreBlock that will handle both step 2 and three.
First, create a MoreBlock that takes in a single Number â I am going to call it displayDice. Then, inside the created MoreBlockâs logic, letâs complete our logic! This example already provides all the necessary images for you.
Lastly, inside the buttonâs onClick event, we want to add our freshly created displayDice block â like this:
That completes all of our logic steps. Click on RUN to see your app come to life!
Conclusion
Today, we learned the purpose of MoreBlock, and how we can utilize it to clean repetitive logic. It can come in very handy when your project gets big in size. Here is a challenge for you guys:
Create two dice that show two different numbers
This could come in handy when youâre playing a board game like Monopoly! Thatâs it guys. Thank you and happy coding! đ˛ đ