Updating the Reset button

Don't think that we have forgotten about our bug! Have you figured out what the bug is?" In the last chapter, we programmed the Reset button to move the ball to the top of the screen (y=2) and to a random x value. But, before the Play button starts the ball moving, it also moves the ball to a random x location. And the problem is, it isn't the same location that the Reset button previously set. So the ball moves once upon Reset and again on Play. We can fix this problem!" START A NEW PARAGRAPH Start it with "In addition to fixing the bug, we have some other updates to the Reset and Play buttons. Now that we have added scoring and leveling to our game, we need to edit the Reset button. There are several tasks related to resetting the game. They are as follows:

  1. Stopping the ball's motion and resetting the position of the ball.
  2. Resetting the score variable.
  3. Updating the label used to display the score.

Another point to note is that we can't assume that the only reason a user presses Reset is because the ball hit the bottom edge and the game ended. We also have to think of the scenario of the user pressing the Reset button to stop the game.

To reset the position of the ball, we will reuse the Ball1.MoveTo block that we already coded in Chapter 3, Navigating the App Inventor Platform, when we created the Play_Button.Click event. When the user presses Play button, the ball will move to a random x coordinate between the value of 1 and the screen width.

Updating the Reset button

You may also recall in Chapter 3, Navigating the App Inventor Platform, that we programmed the Reset button to move the ball to a random integer between 1 and the screen width, as shown in the following screenshot:

Updating the Reset button

As the app is currently programmed, the Reset button moves the ball to a random x position at the top of the screen and the Play button also moves the ball to a random x position at the top of the screen, but these two random positions are not the same. Imagine a scenario in which the ball hits the bottom edge and ends the game. A user presses Reset to move the ball back up to the top of the screen. It goes to a random x location. Then, when the user presses Play, the ball is again reset to a new random x location before it starts moving downward.

We first programmed the Play button to position the ball at the top of the screen, because the first time the game is played, the user wouldn't press the Reset button, but rather the Play button. However, after the game is played once, the user would press Reset and thus the Play button would no longer need to reposition the ball, since the Reset button will perform this function.

We want the Play button to let the ball start moving from the same location that the Reset button previously randomly selected. To do this, we need to program our app so that the Play button gets the ball's x location from the Reset button.

To make this Reset_Button and Play_Button communication happen, we will first create a variable called randomX. This variable will initially be set to 0. Initializing a variable to some value (even if that value is not used later on) is important. Select the initialize global name to block from the Variables blocks drawer. Change the name to randomX. Select a number Math block and attach it, as shown in the following screenshot (note that this set of blocks stands on its own; it does not fit into the other event blocks):

Updating the Reset button

After initializing the global variable, randomX, we can now use it for both our Reset and Play buttons. If you recall, when we programmed the Play_Button.Click event, we enabled the ball to start moving. We set the value of the set Ball1.Enabled block to true, as shown in the following screenshot:

Updating the Reset button

So, for the Reset button, we want to disable the ball. And we do this by making the value false.

First, copy and paste the set Ball1.Enabled block; it will also copy and paste the attached true block. Simply click on the arrow to the right of the word true and you will be able to select false. This block disables the ball's movement. This was one of our goals, since the user will expect the game to stop when the Reset button is pressed.

Next, select the set global randomX block from the Variables block (it will be available as a choice in the blocks drawer, since we initialized the global variable, randomX). We will generate a random integer for the x coordinate and store it in this randomX variable. You know how to do this because we have already created the blocks to move the ball to a random x integer. Copy and paste the random integer from to blocks and add them to the set global randomX to block, as shown in the following screenshot. Once we set the global randomX variable to a random integer, we will use this variable in the Call Ball1.MoveTo block. The following screenshot shows how to generate a random number, store it in the variable (set global randomX), and then use this variable (get global randomX). This completes Task 1.

Updating the Reset button

Tasks 2 and Task 3 are relatively easy. Since the Reset button also resets the score to equal zero, we will also set the score variable to zero. To do this, we will copy and paste the set global score block from the Ball1.EdgeReached event and modify the right-hand side to simply be a Math number block of the value 0. For Task 3, we will simply copy and paste the Score_Level_Label.Text block that we created previously in this chapter. This block always updates the label using the latest value of the score and the level. Since we are resetting the score to 0 right before we execute this block, this block will correctly reset the label to show 0 for the score and the level. (Note: blocks execute from top to bottom.)

Updating the Reset button
..................Content has been hidden....................

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