web analytics

Processing:条件式

1. 布尔表达式通常被称为条件式(conditionals)。

2. 条件式链(各种else if保准让你晕头转向):
if (boolean expression #1) {
// 如果布尔表达式 #1 为真时执行的代码
} else if (boolean expression #2) {
//
如果布尔表达式 #2 为真时执行的代码
} else if (boolean expression #n) {
//
如果布尔表达式 #n 为真时执行的代码
} else {
// 如果以上布尔表达式都为假时执行的代码
}

3. 记住一点就ok,当你的一堆条件式中的某条为真,则此条相应的代码将被执行,剩余的条件式同时被忽略。

4. 一个条件式中只能含有一对if和else,但却可以包含无限个else if。

5. 一个等号:赋值给一个变量;两个等号:某某和某某相等。

6. constrain()函数应用
if (r > 255) {
r = 255;
} else if (r < 0) { r = 0; }

等同于
r = constrain(r,0,255);

7. 发现一点,在做一个例子的时候:方块向右滑动,到X轴等于100的时候停止,我这么写:
if (x>99) {
x=99;
} else {
x=x+1;}
}

方块停是停下来了,但是却在不断左右颤抖,后来改成这样:
if (x>99) {
x=100;
} else {
x=x+1;}
}

正常。

当然,最简单的方式还是x= constrain(x,0,100);

8. 类似&&以及||这些逻辑表达式可以写在if的同一行,例如if(mouseX < 100 && mouseY < 100)

Exercise 5-6
Exercise 5-6

9. 练习5_6,把运行窗口分为四个矩形,鼠标离开之前停留的方块后颜色由白渐变入黑。低智商的我没有日出来,看了答案后发觉和我的思路完全不一样,而且我总觉得有其它实现方法(虽然Daniel老头的方法也并不复杂。它的关键点:设置每个方块的亮度一直在递减,注意是一直)。源文件.

10. 按钮的用法:

boolean button =  false;

int x = 50;

int y  = 50;

int w   = 100;

int h  = 75;

void setup()  {

size(200,200);

}

void draw()  {

if (mouseX >  x && mouseX < x +  w && mouseY >  y && mouseY <  y + h && mousePressed) {

button     =       true;

}  else{

button     =       false;


}

if (button)

{

background(255);

stroke(0);

}  else{


background(0);

stroke(255);

}

fill(175);

rect(x,y,w,h);

}

11. 点一下,换背景色,再点一下,换回来:

boolean button = false;


int x = 50;

int y = 50;

int w = 100;

int h = 75;


void setup() {

size(200,200);

}


void draw() {

if (button) {

background(255);

stroke(0);

} else {

background(0);

stroke(255);

}


fill(175);

rect(x,y,w,h);

}


// When the mouse is pressed, the state of the button is toggled.

// Try moving this code to draw() like in the rollover example.  What goes wrong?

void mousePressed() {

if (mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h) {

button = !button;

}

}

12.  一个设定布尔变量的例子,鼠标点击后,球从左边移动到右边。先设定布尔变量going为假,再设定鼠标点击后(void mousePressed())going为真,然后设定going为真时circleX=circleX+1;。源文件

13. 这里说一下布尔变量,布尔变量是一个只能为真或假的变量。申明方式为boolean+名称=false;(或true)

14. 在例5-6中,他提出了一个负的速度的概念,意思就是与正的速度相反运行(其实就是当x到达多少的时候,开始递减),这就出现了一个不停左右移动的球:

int x = 0;

int speed = 1;


void setup() {

size(200,200);

smooth();

}


void draw() {

background(255);


// Add the current speed to the x location.

x = x + speed;


// Remember, || means “or.”

if ((x > width) || (x < 0)) {

// If the object reaches either edge, multiply speed by -1 to turn it around.

speed = speed * -1;

}


// Display circle at x location

stroke(0);

fill(175);

ellipse(x,100,32,32);

}

15. speed = speed * -1;等同于speed *= -1;

16. 环环相扣的经典例子5-8,实现一个矩形围绕运行窗口边框运动的效果。巧妙的借用了一个变量state,值得好好学习玩味。源文件。为什么不试试让它反方向转动起来?

17. 例5-9是一个模拟重力加速度的例子:

float x = 100;   // x location of square

float y = 0;     // y location of square


float speed = 0;   // speed of square


// 一个新的变量,地心引力.

// 一般我们都选用一个较小的值(0.1)加以重力加速度上。

float gravity = 0.1;


void setup() {

size(200,200);


}


void draw() {

background(255);


// 画矩形

fill(175);

stroke(0);

rectMode(CENTER);

rect(x,y,10,10);


// 在y轴上加速

y = y + speed;


// 为速度加上加速度

speed = speed + gravity;


// 如果方块到底,则让速度反向

if (y > height) {

// 乘以 -0.95 取代 -1 使得方块每弹跳一下都在减速.

// 这叫做“”回潮“,是一种更真实的模拟现实效果(如果没有它,方块将永远都在弹跳而不会停止)

speed = speed * -0.95;

}

}
后来我把它改了一下,改成从鼠标点击点放下方块,很有意思,你试试?

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注