Time for action reach for a new level

If you did the previous hero exercise, then you have a second ball in your project. If you didn't do the hero exercise, you'll need to finish it before you can continue:

  1. Select ball2 from the sprites list so we can begin customizing it.
  2. We need to hide ball2 at the start of the game, so add a hide block between the when flag clicked and the go to blocks.
  3. Next, we want to display ball2 after 30 seconds has elapsed. From the Control palette, add a wait until block between the go to and wait until touching color red blocks.
  4. From the Operators palette, add the greater than block (see the following screenshot) to the input value of the wait until block we just added to the script.
    Time for action reach for a new levelTroll pongsecond level, adding
  5. Our greater than block needs two values. For the first value, add the timer block from the Sensing palette. Then, select the checkbox next to the timer block to report the time on the stage.
  6. For the second value in our greater than block, type 30.
  7. After the timer is greater than 30 block, we need to show ball2. Add a show block between the two wait until blocks.
  8. Set the direction of the ball. From the Motion palette, add the point in direction block between the show and the go to blocks. Change the value to -45.
  9. We need to ensure that the timer starts from 0 at the start of each game. Select the Stage. From the Sensing palette, add the reset timer block between the when flag clicked and forever blocks.
    Time for action reach for a new levelTroll pongsecond level, adding
  10. Click the flag and play the game. After 30 seconds, the second ball will display.
Time for action reach for a new levelTroll pongsecond level, adding

What just happened?

If we were good enough, we lasted for 30 seconds and advanced to the next level in our game.

To determine when we reached level two, we did a simple arithmetic problem. We waited until the value of the timer was greater than 30 until we showed ball2. After ball2 displayed on the stage, the point in direction block set the ball's initial path to -45.

We reset the timer to 0 because the timer continually runs without any regard to the rest of the project. If we didn't reset it to 0, then our game would always start with two balls. That's not the objective.

Comparisons

Scratch includes three ways in which we can compare values: greater than, less than, or equal to. We can compare any combination of constant and variable values, including two variables.

When Scratch compares two values, it returns either true or false. The statement 5 = 4 returns false, while 6 < 10 returns true. Remember, when we use a variable for one of the values, the variable really contains a number.

If we compare values within a conditional statement, Scratch executes the code only if the statement is true.

Troubleshooting

If you played long enough, you might have discovered a bug in our program. Sometimes when the balls collide, the program stops running, and sometimes the balls appear to pass through each other without any problems.

Can you determine the problem? My first thought was that the X and Y coordinates of the two balls matched. The following screenshot shows a scenario where my scripts stopped running, unexpectedly. However, the coordinates of the two balls are clearly different.

TroubleshootingTroll pongsecond level, adding

What do we know about the reason our scripts stop running? We have a code associated with ball2 that says, wait until ball2 touches the color red, then stop all scripts. If you look at the previous screenshot, what color shows on the ball? The color red is displayed.

..................Content has been hidden....................

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