Retrieving Mouse Button and Coordinate Status

In many types of applications, the mouse-pointer is used to control the movement of sprites and to affect the operation of the application in many other different ways. The sensing blocks shown in Figure 6.1 provide access to data about the operation of the mouse-pointer.

Figure 6.1. These sensing blocks report on the mouse-pointer’s coordinates and button status.


The first of these three code blocks retrieves the location of the mouse-pointer as it moves along the X-axis. As was stated in Chapter 2, “Getting Comfortable with the Scratch Development Environment,” Scratch supports a total range of –240 to 240. The second of these code blocks retrieves the location of the mouse-pointer as it moves along the Y-axis. Scratch supports a total range of 180 to –180 on its Y-axis. The third code block is used to retrieve a true/false value that identifies when the mouse’s button is being pressed. The following script, which is part of a drawing application, demonstrates how to work with all three of these sensing code blocks.

To create the drawing application, create a new Scratch application project. Remove the cat sprite from it and then create and add a new sprite that consists of a single black dot. Next, select the thumbnail representing the dot and then add the script shown above to it.

This application’s operation depends on the use of a virtual pen object that Scratch makes available to you via pen code blocks, which you will learn about in Chapter 12, “Drawing Lines and Shapes.” The overall operation of the application is controlled by the script, which automatically begins executing when the green flag button is clicked. Once started, two pen blocks are used to set the width of the pen and the color used by the pen when drawing. Next, a forever code block has been added to repeat the execution of all the code blocks embedded within it.

Within the loop, an if. . . else code block is used to conditionally control the execution of three additional statements. The if. . . else code block’s execution is controlled by examining the value returned by a sensing block that returns a value of true when the user presses the mouse’s left button and false if the mouse’s left button is not being pressed.

When the user presses the left mouse button, the two statements located at the top of the if. . . else code block are executed. The first statement moves the sprite to the same location as the pointer, and the second code block places Scratch’s virtual pen in a down position, allowing drawing to begin. As a result, a blue line is drawn anywhere on the stage where the mouse-pointer is moved when the left mouse button is being pressed. The code block located at the bottom of the if. . . else code block is executed whenever the user releases the left mouse button, lifting the virtual pen and halting any drawing operations.

Figure 6.2 demonstrates the operation of the drawing application.

Figure 6.2. An example of the drawing application in action.


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

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