加工类油漆程序中的色彩系统

时间:2015-10-29 02:35:27

标签: variables while-loop processing paint processing.js

我正在处理中建立一个类似绘画的程序。我希望能够调整笔颜色的r,g和b值。我所做的是使用'r'键允许用户更改r。点击“r”后,他们使用“+”和“ - ”键进行调整。然后你点击'd',它结束了。 '+'和' - '已用于笔尺寸,所以我必须这样做。但是,当我运行代码并点击r时,它会冻结并停止响应。有谁知道什么是错的。

以下是代码中有问题的部分:

if(key == 'r'){ // Activates if 'r' is pressed
  actr = true; // Sets actr = to true
  while (actr = true) { // Goes until actr is false
    if (key == '=') { // Activates is '=' is pressed
      r = r ++; // Increases r by one
    }

    if (key == '-'){ // Activates if '-' is pressed
      r = r --; // Decreases r by one
    }

    if (key == 'd') { // Activates if 'd' is pressed
      actr = false; // Sets actr = to false
    }
  }
}

以下是完整代码:http://www.openprocessing.org/sketch/226658

1 个答案:

答案 0 :(得分:1)

你遇到了一些问题。首先,看看这一行:

while (actr = true) { // Goes until actr is false

您不是在此处检查相等性,而是将true的值分配给actr变量,该变量也将评估为true。换句话说,这永远不会是假的。相反,你应该使用:

while (actr == true) {

甚至更好:

while (actr) {

但是,即使你修复了这个问题,你的while循环仍然永远不会退出。这是因为您忙着等待并阻止程序继续。 这会阻止Processing更改key变量。

不要忙于等待,只需跟踪您所处的模式,即确定+和 - 键的作用。你可以使用一系列布尔值:

boolean size = true;
boolean red = false;
boolean green = false;
boolean blue = false;

void keyPressed(){
  if(key == 'r'){ // Activates if 'r' is pressed

    if (key == '=') { 
      if(size){
         x++;
      }
      else if(red){
        r++;
      }
    }
    else if (key == '-'){
      if(size){
         x++;
      }
      else if(red){
        r++;
      }
    }

    if (key == 'd') { // Activates if 'd' is pressed
      size = true;
      red = false;
      blue = false;
      green = false;
    }
    else if(key == 'r'){
      size = false;
      red = true;
      blue = false;
      green = false;
    }
  }
}

这只是一种方法,而且我没有包含所有代码,但这应该是比忙碌的等待更好的一般想法。