Scratch exercise 145

Not mouse down

In this exercise you will learn how to create on-stage buttons to control sprite properties. Properties are things such as size, speed, position and direction.

 

Complete these steps ..

 

1. Open Scratch, OR create a new game (from the top menu choose "File>New").

 

 

2. Select the stage and add these scripts to begin and end your game ..

 

 

3. Select the cat sprite, then re-name it, and then click the "only face left-right" button ..

 

 

4. Create the following variables for the cat ("for all sprites") ..

 

On the stage, drag their displays down and away from the top of the stage for now. You will re-position them later.

 

 

5. You need to make sure the cat sprite does not appear in front of the buttons you will be creating. If it does, they will be impossible to click.

Add the following script to the Cat sprite ..

 

 

6. Now you need to create the buttons that will appear on the stage and allow you to change the Cat's speed and size when you click on them.

Start with the "speed +" button. When clicked this will make the cat sprite move faster.

Click the "Paint new sprite" button under the stage and then use the rectangle and types tools to create a simple button like this ..

 

 

7. Name the button "Speed +" and then add the script to set it's size and position ..

 

If you press "B" you will see this button sprite move to the top left of the stage.

 

 

8. Now add the scripts which will increase "Cat speed" when the button is clicked ..

 

This script makes the speed slowly increase until it reaches 20 or you release the mouse button. Because there is not a "mouse up" script block, we use "repeat until not mouse down".

 

Switch to full screen mode (this is necessary when you want to test something that is clicked,) and click the "Speed +" button. The speed variable will increase but the cat sprite won't move yet. Notice how the speed increases smoothly as you hold down the mouse button.

 

 

9. Use "File>Save as" to name and save your game. Name it "Not mouse down" ..

 

 

10. Now you need to create a button to reduce the speed.

Right-click the "Speed +" sprite thumbnail under the stage to duplicate it. Name the duplicate "Speed -"

Edit its costume to look like this ..

 

Now add the first script ..

You may need to adjust the x value (-132) to get the buttons arranged next to each other without a gap like this ..

 

 

11. Add the second script to the "speed -" button ..

 

 

12. Finally, right-click the speed variable display and choose "large readout" ..

Now drag and position it next to the speed buttons ..

 

 

13. Select the Cat sprite and add the script to control it's speed and movement ..

 

Go full screen and then press the "B" key on your keyboard to test the button.

Press the "E" key to end your game and continue working on it.

 

 

14. Use "File>Save" to re-save your game ..

 

 

15. Now, you need to repeat the processes for the Size buttons. Start by duplicating the "Speed +" button, re-naming it "Size +" and changing the text on its costume ..

 

Here are the scripts you need for the "Size +" button ..

 

Here are the scripts you need for the "Size -" button ..

 

 

16. Press "B" to position the buttons then right-click the size variable display and choose "large readout" ..

Now drag and position it next to the size buttons ..

 

 

17. Select the Cat sprite and add the script to control it's size ..

 

Go full screen and then press the "B" key on your keyboard to test the size buttons.

Press the "E" key to end your game and continue working on it.

 

 

18. Use "File>Save" to re-save your game ..

 

Well done, you've finished this exercise. You can use these buttons and scripts in any game you make. Right-click the sprite thumbnail image under the stage to export a sprite with all its scripts.

 

Can you make more buttons to control additional properties such as Color effect?