更改路径颜色时打印文本 onclick
Print text when changing color of path onclick
我正在尝试制作一款需要点击圆圈的游戏。当你点击它时,每个圆圈都会改变颜色。我希望它在所有圆圈都被点击时显示 "congrats" 但此刻它显示 'congrats' 就在一个圆圈被按下之后。知道我该如何解决这个问题吗?
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;
}
}
}
我认为您应该针对 text.visible
属性 检查 counter
,而不是填充颜色。这有意义吗?
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;
}
}
});
实际上,您需要更改:
balls[j].fillColor === 'coral';
至:
balls[j].fillColor.equals('coral');
因为 fillColor 是一个对象,你需要使用 "equals" 方法来比较这样的字符串颜色:http://paperjs.org/reference/color/#equals-color
我正在尝试制作一款需要点击圆圈的游戏。当你点击它时,每个圆圈都会改变颜色。我希望它在所有圆圈都被点击时显示 "congrats" 但此刻它显示 'congrats' 就在一个圆圈被按下之后。知道我该如何解决这个问题吗?
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;
}
}
}
我认为您应该针对 text.visible
属性 检查 counter
,而不是填充颜色。这有意义吗?
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;
}
}
});
实际上,您需要更改:
balls[j].fillColor === 'coral';
至:
balls[j].fillColor.equals('coral');
因为 fillColor 是一个对象,你需要使用 "equals" 方法来比较这样的字符串颜色:http://paperjs.org/reference/color/#equals-color