20.03.2013 Views

Introduction to Computer Science with Scratch and App Inventor

Introduction to Computer Science with Scratch and App Inventor

Introduction to Computer Science with Scratch and App Inventor

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Interacting <strong>with</strong> Other Sprites<br />

So far we have used the if block <strong>to</strong> let the user control the actions of our sprite, but we can also use the<br />

if block <strong>to</strong> make different sprites interact <strong>with</strong> each other. First I need <strong>to</strong> add a second sprite <strong>to</strong> my<br />

program. Scripts are sprite-specific. Every new sprite will need its own script(s) <strong>to</strong> tell it how <strong>to</strong><br />

behave.<br />

Now I have two sprites – a dinosaur <strong>and</strong> a bowl of<br />

cheesy poofs.<br />

I can control my dinosaur using the arrow keys on<br />

the keyboard using the script I wrote before.<br />

Let’s try <strong>to</strong> make the dinosaur eat the cheesy poofs!<br />

First, we need <strong>to</strong> move the dinosaur <strong>to</strong> the cheesy<br />

poofs – that is easy <strong>to</strong> do using the arrow keys<br />

The second step is <strong>to</strong> write a script so that we know<br />

when the dinosaur arrives at the cheesy poofs.<br />

When the dinosaur gets <strong>to</strong> the cheesy poofs, we<br />

want <strong>to</strong> make them disappear so that it looks like<br />

they’ve been gobbled up.<br />

Let’s write a script for the cheesy poofs that makes it look like the dinosaur is eating them. First, <strong>to</strong><br />

detect when the dinosaur gets <strong>to</strong> the cheesy poofs we can use an if block. What should the condition<br />

be? Luckily, one of the sensing blocks will tell us when two sprites are <strong>to</strong>uching each other. Here, we<br />

want <strong>to</strong> pick the condition “if <strong>to</strong>uching sprite1” since the dinosaur is sprite1.<br />

Check down here <strong>to</strong> see what each sprite is named.<br />

You can change the name of your sprites if you like.<br />

mbrace Game Development Workbook Page 18

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!