Students keep one star's place and speed in an object, read and change properties with a dot, make a list of objects with push and random, move them with a for-of loop, find a speed missing its star., and add a property and a second list of their own.
Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.
Predict and Run 10 min · Investigate 15 min · Modify 10 min · Make 20 min
Read the unit primer first. This is the Python Objects with turtles unit in JavaScript, without classes: one object per thing, a list of objects, a loop. { x: 20, y: 30 } is an object; star.y reads and changes a property. for (let star of stars) is the easy loop: no counter, and star is the object itself, so star.y = ... changes the real star. The classic slip is writing speed instead of star.speed; the playground's hint for "x is not defined" suggests spelling, so say the dot aloud when you meet it. The Predict uses console.log so students compare a printed number: the Output box sits under the sketch. This lesson uses random and push from the last one without re-teaching them.
The dot: star.speed, not speed. Say it as 'this star's speed'.
| What you will see | A question to ask | The fix |
|---|---|---|
| A red line: speed is not defined. | Whose speed is it? | Write star.speed. |
| Only one star appears, or they all move together. | Do the stars have different x values and speeds? | Make x depend on i in the setup loop, and use random for speed. |
| A red line: Cannot read properties of undefined. | Is the loop reading a property of something that is not in the list? | Check the list name in the loop is the list you pushed to. |
| The new list's objects are never drawn. | Does draw() have a loop over that list? | Add a for (let item of list) loop in draw() for it. |
One possible solution for each task. Other correct programs are fine.
let stars = [];
function setup() {
createCanvas(300, 200);
for (let i = 0; i < 12; i++) {
stars.push({ x: 15 + i * 24, y: random(200), speed: random(1, 3) });
}
}
function draw() {
background(20, 30, 60);
fill(255, 210, 0);
for (let star of stars) {
circle(star.x, star.y, 12);
star.y = star.y + star.speed;
if (star.y > 210) {
star.y = -10;
}
}
}
let stars = [];
function setup() {
createCanvas(300, 200);
for (let i = 0; i < 6; i++) {
stars.push({ x: 25 + i * 50, y: random(200), speed: random(1, 3), size: random(6, 18) });
}
}
function draw() {
background(20, 30, 60);
fill(255, 210, 0);
for (let star of stars) {
circle(star.x, star.y, star.size);
star.y = star.y + star.speed;
if (star.y > 210) {
star.y = -10;
}
}
}
let stars = [];
let clouds = [];
function setup() {
createCanvas(300, 200);
for (let i = 0; i < 6; i++) {
stars.push({ x: 25 + i * 50, y: random(200), speed: random(1, 3) });
}
for (let i = 0; i < 3; i++) {
clouds.push({ x: random(300), y: 30 + i * 50, speed: random(0.3, 1) });
}
}
function draw() {
background(20, 30, 60);
fill(200, 200, 220);
for (let cloud of clouds) {
ellipse(cloud.x, cloud.y, 60, 20);
cloud.x = cloud.x + cloud.speed;
if (cloud.x > 330) {
cloud.x = -30;
}
}
fill(255, 210, 0);
for (let star of stars) {
circle(star.x, star.y, 12);
star.y = star.y + star.speed;
if (star.y > 210) {
star.y = -10;
}
}
}
Stretch: Make the stars twinkle by changing their size a little every frame, or give each one a colour property chosen with random.
Support: Give students the cloud objects and loop already written, and ask only for the stars' size property.
What does star.speed mean inside the loop?
The speed property of the star the loop is currently on.
Next lesson: functions with parameters, so restarting a star, or drawing a flower, is code with a name.