In this step, you will change the appearance of a sprite and the rover to show the rover collecting samples.
Look at the rover sprite’s costumes. There are six animations available. The rover can:
When you want to organise a lot of code in Scratch, such as several costume changes, it is useful to use My Blocks
. This allows you to create your own custom blocks.
Your rover sprite will have a My Blocks
for each animation.
In the
menu, click on Make a Block, and name your new block My Blocks
sample fruit
.
A new block should appear in your script. It will look like this:
Beneath this block, attach some switch costume
blocks and wait
blocks, to animate the robot.
Tip: It is quicker to create your first switch costume
block and wait
block, then duplicate them, and change the costume being used.
Add a block so that the rover sprite plays a sound when it collects the fruit sample.
You can click on the define sample fruit
block to see the animation. If you are on a small screen, you might need to look closely.
The animation won’t run when you click the green flag though, as you have not yet used your new sample fruit block
in your project.
To use your new block, you can attach it to an event
block. In the My Blocks
menu, you should see the block you made. Use it in the following script.
Click on the rover sprite, and you should see the animation.
Now you need to make the rover actually collect a sample. In this example, the rover will collect a fruit from a tree.
The tree sprite should be edited to give it two different costumes. One with a fruit on (tree with fruit
), and one without a fruit (tree without fruit
). Edit one of the costumes, so that the tree has two different costumes.
On the tree sprite, add blocks to set the costume of the tree at the start of the project, and the costume it should switch to when it receives a sample fruit
broadcast.
On the rover sprite, you can use the new
to trigger the costume change. Add this new broadcast
broadcast
into your define sample fruit
function.
Test: To check that your code is working, click on the flag, and then click on your rover sprite. Its arm should extend, and the tree sprite should change costumes.
Tip: Switch to full screen mode and you will be able to see the animation more easily.
The rover should only be able to collect the fruit, if it is touching it.
On the rover sprite, change the when this sprite clicked
set of blocks, so that the sample fruit
function is only called if the rover sprite is touching the colour of your fruit.
Tip: Your costume change from testing might mean that the fruit is not visible. Just click on the costumes tab for the tree sprite, and switch to the costume with the visible fruit.
Now that the tree sprite changes when a fruit is sampled, you need to reset the sprite to its first costume when it goes off the screen.
Test: Move the rover sprite so that it is touching the fruit, then click on the rover sprite and watch it collect the fruit from the tree.