Level 5

Click and Drag

Level 5 · Chapter 4

What this lesson follows

CSTA K–12 Computer Science Standards (2017)

Algorithms & Programming · Grades 6–8

Combine loops and conditions, even nested ones.

Code 2-AP-12Read the official text

Also practised: 2-AP-13

Common Core State Standards for Mathematics

Geometry · Grade 6

Find area, surface area and volume.

Code 6.G.A.3Read the official text

The short lines are our own words. We matched this lesson to these standards ourselves; the standards' owners have not reviewed or endorsed this course.

Common Core State Standards © 2010 National Governors Association Center for Best Practices and Council of Chief State School Officers. All rights reserved.

Today you will detect mouse button state and build drag-and-drop interaction.

1👋 Warm-Up

For teachers · goals · guide
Lesson Outcomes · for teachersCSTA 2
✦ CSTA 2-AP-12 · CSTA 2-AP-13 · ISTE 1.5.c · Standards
🎯 I candetect mouse button state and build drag-and-drop interaction
🧩 New blocks3 blocks
📝 New words7 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 touching condition.
    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-back glide then runs exactly once per drag, not every frame. Do not put the glide in an else branch of the forever-if: it would fire every frame Pico sits idle and pin Pico to the start position.