Time for action - change direction

Let's calculate a new direction:

  1. Select the ball sprite from the sprites list, and find the script that contains the point in direction block. See the following screenshot:
    Time for action - change direction
  2. Change the calculation used to redirect the ball. Change 180 to 90.
  3. Click the flag to test. Observe the results.
  4. Let's simplify the direction change. Drag the green number block that currently contains the calculation 90 direction out of the point in direction block. You can drag it down to a blank spot in the scripts area.
  5. Change the value of the point in direction block to 180. Click the flag to play. Observe the results. The ball should always fall through the paddle and to the bottom of the screen.
  6. Change the value of the point in direction block to 0. Click the flag to play. This time the ball bounces back up when it hits the paddle.
Time for action - change direction

What just happened?

We definitely had some variations in our game. Using 90 - direction did not produce a playable game. When we simplified the direction to 180, our game exhibited a similar unplayable behavior, but a setting of 0 seemed to work.

In the original point in direction block calculation, 180 is a constant and we subtracted the direction of the ball, which was stored in the direction block, a variable, to get a new direction value.

Here's an example calculation that uses 121 as the ball's direction. The program subtracts 121 from 180 to get 59. Therefore, the value of the point in direction block becomes 59.

The original calculation always seems to bounce the ball off the paddle and put it in play. When we changed our calculation to use 90 - direction, the ball's behavior became much more erratic, and it may have failed to bounce off the paddle.

When we simplified our direction, we tried setting a constant value of 180, but if you recall our direction discussion from Chapter 5, you will remember that 180 represented a down direction. When we set the direction to 0, the ball bounced off the paddle as expected.

The point in direction block is not the only calculation in the script that affects the ball's direction. Let's take a closer look.

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

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