Computer Discoveries, Unit 3, Lesson

Hi, I’m a little confused about a bit of code for CSD Unit 3, Lesson 7, Step 11:

var greenAlien = createSprite(100, 200);
greenAlien.setAnimation(“greenAlien”);
var pinkAlien = createSprite(300, 200);
pinkAlien.setAnimation(“pinkAlien”);

// Setting Up Drawing
noStroke();
fill(“white”);

function draw() {
background(“black”);
ellipse(randomNumber(0,400),randomNumber(0,400),5,5);
fill(“red”);
pinkAlien.rotation = randomNumber(-5, 5);
greenAlien.rotation = randomNumber(-5,5);
drawSprites();
}
I initially set my fill as white but then change it to red. But my ellipses stay white. Why?

Link to the project or level: https://studio.code.org/s/csd3-2019/stage/7/puzzle/11
What I expect to happen: stars should change fill color from white to a new color
What actually happens: stars stay the same color as outside the draw function
What I’ve tried:

Hi @rkane,

So, the order of the blocks still matters even inside the draw loop. Have you tried changing the order of your blocks a little?

Mike

Hi Mike
Thanks for responding. My thought process is that outside the draw loop, I’ve initially set the fill color to white. So when we 1st step into the draw loop, the first call of ellipse will be white. But then I change fill to red, so the 2nd and subsequent loops through the draw function should draw the ellipse as red, shouldn’t it? Or does it keep reverting back to the call to white each time we start the draw loop?

Logically, your explanation makes sense but my understanding is that each time the Draw loop is invoked, it is using default values (from outside the loop) until you define them otherwise, so it isn’t remembering commands from one iteration of the draw loop to the next.

Mike

Right on. That was a little nuance that I didn’t get. Thanks Mike!

1 Like