Chapter 4. Fling App – Part 2

In Chapter 3, Navigating the App Inventor Platform, you learned to use the MIT App Inventor Designer and Blocks Editor by creating your first fully functional mobile app! In this chapter, we will show you how you can take the basic Fling app and build it out by adding more complex features. We will demonstrate how to enable:

  • A scoring mechanism that will display when the ball touches the top edge
  • Code, so the ball can only be flung from the lower half of the screen
  • The increasing levels of difficulty by increasing ball speed

We will also begin debugging. Debugging is a standard practice in app development and should be viewed as part of the process, not as something negative.

Each time you add new components to your app, we encourage you to share your app with others to get feedback. Not only will the feedback give you ideas about the design, but it will also help you learn what users want from an app game. Such information will prove invaluable once you begin designing apps from scratch. This chapter will not only help you discover new ways to enhance a game app, it will also trigger new paths of creativity! By the end of this chapter, your Fling app will resemble the following image:

Fling App – Part 2

Adding a scoring feature

Since most game apps include some sort of scoring feature, we will add this code to our Fling app. The score will display in the same label that will also display the Game Over text.

Coding scoring blocks

We left off in the last chapter with the ball bouncing off all the edges except the bottom edge. Now, we want to program the app to increase the score by one point every time the ball reaches the top edge. We will use another if/then block and add it into the else portion of the existing if/then/else block within the when Ball1.EdgeReached event, as shown in the following screenshot:

Coding scoring blocks

Can you guess what block will fit in the if opening? If the ball hits the top edge, then the app will increase the score by 1. In Chapter 3, Navigating the App Inventor Platform, you learned that the bottom edge is represented by -1 (and the top edge is represented by 1). Since we've already created an if/then scenario for the ball hitting the bottom edge, we can copy this set of blocks and adjust it for the ball hitting the top edge.

Copy the blue block from the existing if block (note that by copying the blue block, you will also automatically copy the embedded orange get edge block and the blue -1 math block). Snap these pasted blocks into the new if block and change -1 to 1, as shown in the following screenshot:

Coding scoring blocks

We have coded: if the top edge is reached, then what will happen? We want the app to record a point. To code the score feature, we will first create a global variable. A global variable is a value that can be used by any block, whereas a local variable is one that can only be used by the specific block for which it was intended. Go to the Variables block drawer and select the orange, initialize global name to block.

Coding scoring blocks

Click on name and change the text to score. Next, in the Math drawer, select the 0 blue Math block. Connect it to the initialize global score to block. Your blocks will now look like the following screenshot. Note that this initialize global name block does not fit into any other block; it stands alone with the attached Math 0 block.

Coding scoring blocks

By setting the global variable equal to 0, essentially, we have established the initial score to be set to 0. Now, we can program the app to add 1 to this score. Go back into the Variables block drawer and select the set to block. Click on the little arrow in the middle of the block and select global score from the drop-down menu (this is now available for us to select because we created the global variable). Insert the block into the empty then slot, as shown in the following screenshot:

Coding scoring blocks

You will insert a Math block into the set global score to block because, every time the top edge is reached, we will want to get the current score (in this case, 0) and add 1 to it. Therefore, we will need the blue Math addition block, as shown in the following screenshot:

Coding scoring blocks

The first blank will be filled with an orange get global score block, in the second block after the plus sign, there will be a number block filled with 1, as shown in the following screenshot. There are two ways to find the get global score block. One is in the Variables drawer. Click on the get to block and then click on the downward arrow to select global score from the drop down menu. Or, you can hover (not click) over the initialize global score block and both a set and get global score block will appear. Select the get global score block. The reason we are using the get global score block and not the 0 block is because we want to add 1 to the most recent score. At the beginning of the game, the score is 0, but as soon as the ball hits the top edge, the new global score will be 0+1, then 1+1 (and so on):

Coding scoring blocks

So now, we have created the code to update the score each time the top edge is reached. But we haven't yet created the code to display the score. Let's do this next.

Updating the score label

Take a look at the blocks in the first then section in the following screenshot and notice the name change of our Score Label. Since the Score Label will be displaying both the score and the level, we decided to make that clear in our label name. In the Designer, we renamed the Label from Score to Score_Level_Label. This change updates in the Blocks Editor as well. The block name initially was, set Score.Text to is now, set Score_Level_Label.Text to. The blocks drawer is also updated. We show you this edit in the middle of development because sometimes, no matter how well thought out your app is, you may discover ways to improve it as you begin coding. Changing the Label name is not necessarily integral to the functioning of our app, but it helps us to be more clear in our design

Updating the score label

Since the Label will now display both the score and the level, we need to code that. Copy the green set Score_Level_Label.Text to block, from the if/then/else block. Since it will also copy the pink "Game Over" text block, you can merely delete this block as we won't need it. Instead, go into the pink Text blocks and choose the join block, as shown in the following screenshot:

Updating the score label

We need this block to set the Label text to display pieces of information: the score and the level. If you think about it, we can't just display two numbers because the user won't know what they mean. We have to display: the word Score and the actual score (whatever number it is) and the word "Level" and the actual level (whatever number that is). To begin, we will just add two things, the word Score and the actual score. You probably can guess that the first block to attach to the join block is another blank Text block will. Type the word Score: with a space after the colon (so there will be a space between the word and the score number). The second block is the orange get global score block.

Updating the score label
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset