如果布尔变量为真,递增 i
If Boolean variable true, increment i
我有两个布尔变量 var lookingLeft = false
、var lookingRight = false
和一个椭圆 ellipse(40, 40, i, i)
.
var lookingLeft = false;
var lookingRight = false;
function draw() {
let i = 30;
ellipse(40, 40, i, i);
if (nose.x > leftEye.x) {
lookingLeft = true;
}
if (nose.x > rightEye.x) {
lookingRight = true;
}
if (lookingLeft === true) {
i = i + 10 //this is not working
rect(10, 10, 50, 50); //but this is
}
if (lookingRight === true) {
i = i - 10 //again, this is not working
rect(300, 300, 50, 50); //but this is
}
}
我希望i
在lookingLeft = true
时增加10,在lookingRight = true
时减少10。
这是我的 p5 网络编辑器草图:https://editor.p5js.org/saskiasmith/sketches/7WMDPGPbrc
非常感谢!
您可以通过支票添加。
i += lookingLeft && -10 || lookingRight && 10;
或者取查找标志的增量。
i += 10 * (lookingRight - lookingLeft);
您在计算 i
之前触发椭圆函数。
一旦 ellipse
被触发,你对 i
,函数 运行 做什么都没有关系。
将呼叫移到 draw
的后面,它应该可以工作。
var lookingLeft = false;
var lookingRight = false;
function draw() {
let i = 30;
if (nose.x > leftEye.x) {
lookingLeft = true;
}
if (nose.x > rightEye.x) {
lookingRight = true;
}
if (lookingLeft === true) {
i = i + 10 //this is not working
}
if (lookingRight === true) {
i = i - 10 //again, this is not working
}
ellipse(40, 40, i, i);
}
我不知道这是不是有意为之,但你每次都在重新分配 'i' 开火,所以你可以获得的最大值是 40,最小值是 20。你可以做这样的事情:
if(lookingLeft){ // if it's true it runs, you don't really need "===" with bools in if()
i += 10 //i = i + 10
} else if (lookingRight){
i -= 10
} else{ // OR else if(!lookingLeft && !lookingRight) {}"!" just means NOT so "if not looking left..."
i = 30
}
// and also there are more prettier ways to do this: *I "explain" in the lower text*
i += (lookingRight) ? -10 : 10
if(!lookingRight && !lookingLeft){i = 30}
如果你不明白,我不建议你现在在你的代码中使用它。但我不妨尝试解释一下:
'(lookingRight)' 只是 if 语句的东西,'?'与 'if' 相同,如果为真,则为“-10”,如果不为真,则为“10”,但同样这无关紧要,我不建议使用此...
我也不知道这是否就是您的意思,如果这对您有帮助,希望对您有帮助。
我有两个布尔变量 var lookingLeft = false
、var lookingRight = false
和一个椭圆 ellipse(40, 40, i, i)
.
var lookingLeft = false;
var lookingRight = false;
function draw() {
let i = 30;
ellipse(40, 40, i, i);
if (nose.x > leftEye.x) {
lookingLeft = true;
}
if (nose.x > rightEye.x) {
lookingRight = true;
}
if (lookingLeft === true) {
i = i + 10 //this is not working
rect(10, 10, 50, 50); //but this is
}
if (lookingRight === true) {
i = i - 10 //again, this is not working
rect(300, 300, 50, 50); //but this is
}
}
我希望i
在lookingLeft = true
时增加10,在lookingRight = true
时减少10。
这是我的 p5 网络编辑器草图:https://editor.p5js.org/saskiasmith/sketches/7WMDPGPbrc
非常感谢!
您可以通过支票添加。
i += lookingLeft && -10 || lookingRight && 10;
或者取查找标志的增量。
i += 10 * (lookingRight - lookingLeft);
您在计算 i
之前触发椭圆函数。
一旦 ellipse
被触发,你对 i
,函数 运行 做什么都没有关系。
将呼叫移到 draw
的后面,它应该可以工作。
var lookingLeft = false;
var lookingRight = false;
function draw() {
let i = 30;
if (nose.x > leftEye.x) {
lookingLeft = true;
}
if (nose.x > rightEye.x) {
lookingRight = true;
}
if (lookingLeft === true) {
i = i + 10 //this is not working
}
if (lookingRight === true) {
i = i - 10 //again, this is not working
}
ellipse(40, 40, i, i);
}
我不知道这是不是有意为之,但你每次都在重新分配 'i' 开火,所以你可以获得的最大值是 40,最小值是 20。你可以做这样的事情:
if(lookingLeft){ // if it's true it runs, you don't really need "===" with bools in if()
i += 10 //i = i + 10
} else if (lookingRight){
i -= 10
} else{ // OR else if(!lookingLeft && !lookingRight) {}"!" just means NOT so "if not looking left..."
i = 30
}
// and also there are more prettier ways to do this: *I "explain" in the lower text*
i += (lookingRight) ? -10 : 10
if(!lookingRight && !lookingLeft){i = 30}
如果你不明白,我不建议你现在在你的代码中使用它。但我不妨尝试解释一下: '(lookingRight)' 只是 if 语句的东西,'?'与 'if' 相同,如果为真,则为“-10”,如果不为真,则为“10”,但同样这无关紧要,我不建议使用此...
我也不知道这是否就是您的意思,如果这对您有帮助,希望对您有帮助。