Today you will detect mouse button state and build drag-and-drop interaction.
1 Warm-Up
2 Predict
3 Try It
4 Plan
5 Build
6 Fix It
7 Extend
8 Math Challenge
For teachers · goals · guide
Lesson Outcomes · for teachersCSTA 2
✦ CSTA 2-AP-12 · CSTA 2-AP-13 · ISTE 1.5.c · Standards
| 🎯 I can | detect mouse button state and build drag-and-drop interaction |
| 🧩 New blocks | 3 blocks |
| 📝 New words | 7 words |
| ⏱ Lesson time | ~40 min |
Teacher Guide
📋 Before class
⚠️ Common misconceptions
- Students often use when this sprite clicked (event) for drag behavior. This only fires once at click, not continuously while the button is held. Drag requires checking mouse down state every frame.
- Students may think touching mouse-pointer detects clicks. It detects position overlap — not the button state. Both conditions (mouse down AND touching mouse-pointer) are needed for drag-on-sprite.
🎯 Differentiation
🐢 Struggling: Break drag into two separate moments: (1) detect pickup — mouse is down AND touching the sprite, (2) follow — go to mouse position. Build each part separately before combining.
🚀 Advanced: Challenge students to build a snap-to-grid system where dropped sprites snap to the nearest 40×40 grid cell using round() and math on mouse x/y coordinates.
💡 During the lesson
- Warm-UpDemo dragging a file on the desktop or an item in a mobile app. Ask: "What three things must be true for dragging to work?" (1. Button is held. 2. You started on the object. 3. You keep moving.) These map directly to the three parts of the code. This drag is an interaction 🤝 between the user and the program, and holding the button down is the trigger ⚡ that starts it.
- Try ItRun the basic drag pattern live. Ask: "What happens if you hold the mouse button but move OFF the sprite?" (Usually you can't: Pico jumps to the pointer every frame. Only a very fast flick leaves Pico behind, because touching becomes false.) "What if you want it to keep following even after leaving the sprite?" Preview: remove the
touchingcondition.
The first experiment isolates the AND condition. Mouse-down alone makes any click drag the sprite — not the behavior we want. The AND combination is the exact drag pattern. The third experiment shows how easy it is to layer a small enhancement (reading a value) on top of the core loop. - PlanThe label sprite idea (a separate sprite that reports another sprite's costume name) is a nice design pattern. Ask: "Why might a separate label sprite be more flexible than a variable monitor?" (It can be styled, positioned, and animated.)
- BuildThe
repeat until <not <mouse down?>>loop follows the mouse every frame while the button is held. When the button is released the loop ends — this is the wait-until-release-then-act pattern. The snap-backglidethen runs exactly once per drag, not every frame. Do not put the glide in anelsebranch of the forever-if: it would fire every frame Pico sits idle and pin Pico to the start position.
Show the picture first. Ask what will happen — everyone points or says one word.
Everyone picks an answer first. Then press the flag together.
Change one thing at a time. Run it, watch, then change the next thing.
Pairs put the cards in order before anyone opens Scratch.
Students build step by step. Walk around — help with questions, not with the mouse.
Ask: what did we want, and what did it do? Let them find it.
Fast finishers pick the next tier. Everyone else finishes their build.
Work it out on paper first. Then check it on the stage.