Video - Sprite Movement With Mouse
Let’s begin programming by creating a script that defines the monkey’s behavior.
We will program the monkey to move along with the mouse of the game player.
So, follow the steps below:
Step 1: Open the Scratch application
Step 2: Delete the cat from your screen.
Step 3: Enter the Monkey2 character from Scratch’s library into your project.
Step 4: Change the name of the object to “monkey”.
Step 5: We want the monkey’s move to begin from the moment the game starts.
This can happen by pressing the green flag. From the commands palette “Events”, locate first and then drag with your mouse the command “when the green flag is clicked” and drop it in the space of the scripts’ area.
Step 6: Of course, the object’s movement is defined by a command on the “Motion” palette.
Open this palette and look all the commands, one after the other.
You will find the command “Go to mouse pointer”.
Drag this command to the script area and join it with the previous command, as in a puzzle.
Now, try your script.
The monkey moves to where you have your mouse pointer: the green flag that you have just clicked on.
The movement of the mouse pointer does not mean a corresponding movement of the object. Some change should be made.
Step 8: We want the “Go to mouse pointer” command to be repeated continuously throughout the game process.
The key word here is the word “repeated”. We should use a repeat block.
Let’s listen to what the specialist has to say about this issue.
Often, we want the commands created in Scratch, but also in any programming language, to be running repeatedly.
The existing commands in Scratch that repeat the commands they contain are the following:
- Repeat a specific number of iterations
- Repeat until an event occurs
- Repeat forever
The command “forever” repeats the enclosed commands for the entire duration of the game.
This command, like any of the commands just mentioned, may contain 1, 2 or more other commands executed in order, one after the other.
Once the commands finish, they run again from scratch.
In the command “forever” this continues until the end of the game.
The command “forever” is exactly what we need in this game. We will use this command.
So, drag the “forever” command to the script area and use it this way.
Now, run your project and try the result.
Congratulations! You have just created your first project in Scratch!