O'Reilly logo

Scratch Cookbook by Brandon Milonovich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Getting the mouse position

There are many times where it may be useful to have the mouse position in the program. Perhaps you want an object to follow the mouse, perhaps you want something to happen based on the mouse position, such as a graphic effect.

Getting ready

In this recipe, we're going to create a program that makes our sprite change its graphic effects based on the mouse position.

Start off by creating a new file and importing a new sprite. In our case, we've imported Monkey1 with the white background.

How to do it...

We're going to apply two effects, one based on the mouse's horizontal direction, another based on the mouse's vertical direction. Follow along with these steps:

  1. Drag over a green flag block (from Events).
  2. Drag over a forever

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required