Search code examples
javascriptpaperjs

Print text when changing color of path onclick


I am trying to make a game where you need to click the circles. Each circle changes color when you click on it. I want it to say "congrats" when all circles have been clicked but at the moment it shows 'congrats' just after one circle has been pressed. Any idea how I could fix this?

var text = new PointText(view.center);
text.content = 'Congrats';
text.visible = false;
text.style = {
    fontFamily: 'Courier New',
    fontWeight: 'bold',
    fontSize: 100,
    fillColor: 'black',
    justification: 'center'
}

for (l = 0; l < balls.length; l++) {
    balls[l].onClick = function(event) {
        counter++



    this.fillColor = '#860d2b';
    var areAnyCoral = false;
    for (var j = 0; j < balls.length; j++) {
        if (balls[j].fillColor === 'coral') {
            areAnyCoral = true;
        }
    };
    if (areAnyCoral === false) {
        text.visible = true;
    }

}

}


Solution

  • I think you should be checking against the counter, rather than the fill color, for the text.visible property. Does this make sense?

    var counter = 0;
    var ball1 = {
      color: "notCoral"
    };
    
    var ball2 = {
      color: "notCoral"
    }
    
    var balls = [ball1, ball2];
    
    balls.forEach(function(ball) {
      ball.onClick = function(event) {
        if (ball.color == "notCoral") {
          ball.color = "coral";
          counter++;
        }
    
        if (counter == balls.length) {
          text.visible = true;
        }
      }
    });