Programming a Sprite to Follow a Motion

On this page and the next, you will program two sprites. One sprite will follow your mouse. The other sprite will chase the first. When they meet, they will have a short conversation.

  1. "U1L2-SpriteChase"Create a new project called U1L2-SpriteChase
  2. Read and think about this script before you build it. What do you expect it to do?
    Talk with Your Partner Follow That Mouse Script
    Mouse X Reporter and Mouse Y Reporter report the mouse's location on the stage. Find them in the Sensing palette.
  3. Now build the script and run it. Move your mouse around the stage while the program is running.
    The block at the top of the script is called a hat block. Its shape emphasizes that it can be used only at the beginning of a script. Hat blocks don't say what the script should do; they say what event should start the script running. In this case, the script starts when the green flag button is clicked.

    To stop this script, click the red stop sign: stop button
  4. Does the program work the way you expected?
  5. Create a second sprite by clicking on the add a new turtle sprite button just below the stage.  
    Then use the point towards block to make it point towards Sprite. The animation below shows how. getting a new sprite
  6. The new sprite, named Sprite(2), appears on the stage. It's brand new, so it has no scripts. The buttons beneath the stage let you select the sprite you want to program. Pair Programming Swap
  7. Change your script for Sprite(2) so that once the green flag is clicked, Sprite(2) will point towards Sprite forever.
    pointing towards Sprite forever
  8. While a script is running, its border lights up. If the running time is short, you may not notice the brief flash.
  9. Click green-flag button and check that your script does what you intended:
    • Sprite always follows your mouse.
    • Sprite(2) stays in one place, but always points toward Sprite.
  10. Both sprites' scripts use Whengreen-flag buttonclicked, so both scripts run when green-flag button is clicked.

Now Is a Good Time to Save