Using the Button Control to Initiate Action

In order to work with the Scratch Board’s button control, you must use the first sensing code block shown in Figure 14.2. Using this code block, you can determine whether the Scratch Board’s button control is being pressed. As an example, let’s create another application. Begin by removing the default Cat sprite and then click on the Choose New Sprite from File button, drill down into the Things folder, select the basketball sprite, and click on OK.

Once it is added, select the basketball sprite and add the following script to it:

As you can see, this script begins by positioning the sprite at the center of the stage. It then starts a loop to repeatedly execute a conditional code block that checks to see if the Scratch Board’s button is being pressed. If this is the case, the statements located inside the condition code block are executed. As a result, the image of the basketball is made to bounce. Figure 14.4 depicts how the basketball looks as it begins its upward bounce.

Figure 14.4. Using the Scratch Board’s button to control the bouncing of a virtual basketball.


The basketball will repeatedly bounce for as long as the Scratch Board’s button is being pressed and will stop bouncing as soon as the button is released. Using the previous example as a starting point, you should be able to use a Scratch Board’s button control as an input device for all kinds of Scratch applications. For example, you might use it in place of the mouse button as a means of controlling when to shoot a missile in a Space Invaders-style game.

Reacting to Light

In addition to the slider and button control, you can retrieve input from the light sensor located on your Scratch Board to provide input to your applications. You can use either of Scratch’s two sensing code blocks to interact with the light sensor. To get a better feel of how to work with the light sensor, let’s modify the previous application so that it responds to a change in light in place of the Scratch Board’s button control. To do so, modify the application’s script as shown here.

As you can see, the script has been redesigned so that it only bounces the basketball when the Scratch Board’s light sensor returns a value of 0 (total darkness). To test out the execution of this script, place your hand over the Scratch Board so that it blocks out the light. When you do, the basketball should start bouncing. Remove your hand so that the Scratch Board can detect some light, and the basketball will stop bouncing.

Using this example as a starting point, you could create a Scratch application that performs a certain task only when the lights have been turned off or on. You might also use your Scratch Board as the basis for creating an alarm clock that awakens you when the sun comes up.

Responding to Sound

In addition to providing your application with data based on the amount of light it is able to detect, your Scratch Board can also detect variations in the loudness of sounds. For example, you could easily modify the script belonging to the application that you have been experimenting with to work with sound in place of light.

As redesigned, the script will now bounce the basketball only when the Scratch Board detects a relatively loud noise in the room. The sound sensor returns a range of numbers from 1 to 100, where 0 represents total silence, and 100 represents maximum volume. To see how this change affects your application, start your application and make a little noise. If the basketball does not move, make another noise, this time a little louder. Keep going until you make a noise that is loud enough to trigger the bouncing of the basketball.

Note

The sensing code block shown in the preceding example operates much like the sensing code block shown here.

Unlike this code block, which reports on the loudness of the computer’s microphone, the sensing code block used in the example retrieves its data directly from the Scratch Board’s microphone.


Using sound as a trigger for script execution, you could, for example, create and execute an application that plays an alarm whenever it detects someone in your room, warning him that his presence has been detected, thus creating your own virtual watch dog.

Measuring Electrical Resistance

In addition to working with the Scratch Board’s slider, button, light sensor, and sound sensor, the Scratch Board also comes equipped with four sets of alligator clips, which you can attach to the bottom of the Scratch Board. Each set of alligator clips represents an individual sensor, which you can use to provide your applications with input based on the strength of the electrical resistance in any circuit you set up.

As an example of how you might work with an alligator clip, let’s modify the script for the application that you have been experimenting with, as shown here:

With this modified script now in place, you must touch both ends of the alligator clips together in order to make the basketball bounce. To test how well different materials conduct electricity, you could attach both ends of the alligator clips to different objects to see if enough current passes through to make the basketball bounce. With access to four separate sets of alligator clips, you can create all sorts of different tests and even run them all at once.

Keeping a Watchful Eye on Sensor Data

Scratch allows you to display individual monitors for each of the different types of sensor controls supported by either of the two sensing code blocks that work with the Scratch Board. To do so, click on the Sensing button located at the top of the blocks palette, then click on the drop-down list located in the sensing code block you plan on working with and select the sensor that you want to keep an eye on. Next, select the check box located just to the left of the code block. A monitor for the selected Scratch Board sensor will then appear on the stage. If you want to display additional monitors, you may do so by selecting the code block’s drop-down list again to select a different sensor. You will have to select the block’s check box again. Using this approach, you can display a monitor for as many of the Scratch Board’s sensors as you want, as demonstrated in Figure 14.5.

Figure 14.5. Displaying individual monitors to report on different Scratch Board sensors.


To disable the display of any monitor that you enable, you must perform the procedure outlined above in reverse order to clear out the check box for each sensor. A quicker and easier way of keeping an eye on the data being supplied by multiple sensors is to enable the display of the Scratch Board Watcher, as shown in Figure 14.6.

Figure 14.6. The Scratch Board Watcher lets you keep track of all of the data being supplied by your Scratch Board.


To enable the display of the Scratch Board Watcher, right-click on the sensing code block that you plan to use and select Show Scratch Board Watcher from the popup menu that is displayed. When you are done with the Scratch Board Watcher, you can remove it from the stage by right-clicking on it and selecting Hide from the popup menu.

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

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