Shall We Learn

  • Increase font size
  • Default font size
  • Decrease font size

Scratch Lesson 4: Sprite Interaction via Variables

E-mail

In our previous lesson, we added beat to our dancer sprite. In Lesson 4, I will show you how to add interactions between two sprites.

 

Download the starting project HERE.

View the PDF version of this lesson HERE.

Step 1: Create a Music Sprite

Let’s create a Music Sprite or a sprite that controls playing and stopping of background music. To create a new sprite, click to open Paint Editor.

Instead of drawing our own, this time we will import an image. Click “Import”.

Click “Costumes” if it’s not already selected. Then open “Things” folders.

 

Select “drum1” or whatever image you like. Then click “OK” to save.

Rename this new sprite to “hip-pop”. It’s seems big for me so I shrink it with “Shrink Sprite” tool.

 

Then use “Move” tool to move the “hip-pop” sprite to wherever you see fit. I place it next to dancer1’s feet.

 

Step 2: Move Music Loop to the Music Sprite

Drag the Music Combo Block from dancer1’s Scripts Tab.

Drop it to be over sprite “hip-pop” icon. Wait until the “hip-pop” sprite is highlighted before releasing the mouse.

Sprite “hip-pop” now has the music combo block from “dancer1”. Delete this same combo block from “dancer1”.

Then, IMPORT all three music clips to hip-pop sprite chap4-1 by selecting it from Sprite list, clicking Sound tab, clicking Import button, and selecting Music Loop folder to browse to these three music clips: DrumMachine, HumanBeatbox1, and HipHop.

chap4

Step 3: Turn Music On and Off

To turn music on and off, we need to create a virtual switch. Like a real switch, a virtual switch can be on or off. To create a virtual switch, we use Variables. Select “Variables” Tool Box, and click “Make a variable” button. Then enter “switch” for Variable name.

 

I would like this switch to work this way:

 

When the animation first starts, the music is playing and the switch is on. Click the “dancer1” sprite to turn it OFF, and click the “hip-pop” sprite to turn it ON again. If the switch is ON, music is ON; if the switch is OFF, music is OFF.

 

 

 

For this switch to work, we need following blocks:

  • one block from tool kit

  • one block from tool kit

  • one block from tool kit

 

For sprite, we will add three new combo blocks. The first combo block turns the switch ON (set music on to 1) when is clicked.

 

The second combo block turns the switch ON again (set music on to 1) when “hip-pop” is clicked.

 

Finally, the third combo block keeps checking if the switch is OFF (switch = 0). If OFF, then stops all sounds.

 

 

 

For sprite, we will add one combo block. This combo block will turns the switch OFF if “dancer1” is clicked.

 

Your “dancer1” sprite should have these combo blocks.

 

Your “hip-pop” sprite should have these combo blocks.

Download the result project HERE.

View the PDF version of this lesson HERE.

TAKE A BREAK. I WILL SEE YOU IN LESSON 5.

 

  • Beth Meyer  - Lesson 4 Problem
    As I worked through Lesson Four I could not get mu sic to play after I moved the
    Music Combo box to t he new sprite I found that I had to import the mus ic to t
    he new sprite's sound area in order to get everything to work right. So I belie
    ve that one st ep is missing. Everything else has been fun so far .
  • Jessica Chiang  - Thanks Beth!
    Hi Beth, thank you so much for your comment. I wil l add that missing info in ri
    ght now.
Only registered users can write comments!

!joomlacomment 4.0 Copyright (C) 2009 Compojoom.com . All rights reserved."

Last Updated on Monday, 28 September 2009 10:57