Lesson 18 Example A teacher

The teacher solution documentation shows flower_count=flower_count+1 on line 25, but I keep getting this error message.

your issue lies within the teacher iteration not declaring the increment variable correctly herein I’ve modified the code to probably show what the example was meant to do… not sure if this is best tailored to beginners though but I’m sure you should be able to

var flower1 = createSprite(75, 330, 25, 100);
flower1.setAnimation("flower_1");
flower1.scale = 0.3;
var flower2 = createSprite(150, 330, 25, 100);
flower2.setAnimation("flower_2");
flower2.scale = 0.3;
var flower3 = createSprite(225, 330, 25, 100);
flower3.setAnimation("flower_3");
flower3.scale = 0.3;
// easier to assign collisions within a group
var flowers = createGroup()
flowers.push(flower1, flower2, flower3);
// line to allow incrementor to work properly, this was not defined leading to this result
// undefined = undefined + 1 <= as you know undefined is not a Number datatype or a variable property
var flower_count = 0;
var vase = createSprite(325, 305, 100, 50);
vase.setAnimation("20d7a7af617f388ed55967fda69ba9b1_t-removebg-preview.png_1");
vase.scale = 0.5;
function draw() {
  background("yellow");
  fill("brown");
  noStroke();
  rect(0, 350, 400, 50);
  // loop through the group for collisions
  for (var i = 0; i < flowers.length; i++) {
    var flower = flowers[i];
    if (mouseIsOver(flower) && mouseWentDown("leftButton") && flower.y > 216) {
      flower_count = flower_count + 1;
      // flower.visible = !flower.visible; // can't see the flowers in the vase!
      flower.x = 296 + i * 20;
      flower.y = 216;
      flower.rotation = (i-1) * 15;
    }
  }
  // if (mousePressedOver(flower1)) {
  //   flower_count = flower_count + 1;
  //   flower1.visible = false;
  //   flower1.x = 296;
  //   flower1.y = 216;
  //   flower1.rotation = -15;
  // }
  drawSprites();
}

this program also prevents the flower_count incrementor from increasing from a flower that was already put in the vase hope this helps

Varrience

Hi @tracy.graysonpeters,

I was not able to find this particular example in the teacher solution but @varrience is correct in that the variable was not declared. I am thinking you might have just overlooked it. The error is just saying you are using a variable before you have declared a variable. So, before the draw loop, just add the line:
image
~Michelle

https://studio.code.org/projects/gamelab/OzdFqrZ4oHPYsTddaRWaZxtTvdj9NzZVwCq6NkyONXM/view

This is the exact link from Lesson 18, Bubble 2… Example A The code is from the Teacher’s Solution.

Yes, I evidently did overlook that line of code. Thank you…

I don’t really understand the explanation from Varrience.

Ms. Grayson Peters