处理-通过按“1”键或“2”键来选择形状,以便可以分别拾取形状 1 或形状 2

Processing-A shape is selected by either pressing a key ‘1’ or or key ‘2’ so that shape 1 or shape 2 can be picked up respectively

在草图上画两个形状(例如矩形和圆形)。使用向上、向下、向左和向右键控制 selected 形状的移动。通过按“1”键或“2”键来 select 编辑一个形状,以便可以拾取形状 1 或形状 2 respectively.I 想要 select 通过按键“1”或“2”,但它们不能 运行.`

int x = 0;
int y = 0;
int ex= 0;
int ey= 0;
 
void setup(){
  size (400, 400);  
}
 
void draw(){
  background(80);
  rect(x, y, 25,25);
  ellipse(50, 50, 50, 50);
}
 
void keyPresse() {
  if ( (key == '1')) {
    if (keyCode == UP) {
      y -= 10;
    } else if (keyCode == DOWN) {
      y += 10;
    } else if (keyCode == LEFT) {
      x -= 10;
    } else if (keyCode == RIGHT) {
      x += 10;
    }  
  } else if ((key == '2')){
      if (keyCode == UP) {
      ey -= 10;
    } else if (keyCode == DOWN) {
      ey += 10;
    } else if (keyCode == LEFT) {
      ex -= 10;
    } else if (keyCode == RIGHT) {
      ex += 10;
    } 
  }
}

有错字。键盘回调的名称是 keyPressed。但是,也存在一些逻辑问题。


xy 坐标创建一个数组。和一个索引变量(shape_i):

int x[] = new int[]{100, 100};
int y[] = new int[]{200, 100};
int shape_i = 0; 

在它的位置画出形状。 (x[0], y[0])是矩形的位置,(x[1],y[1])是椭圆的位置:

void draw(){
  background(80);
  rect(x[0], y[0], 25, 25);
  ellipse(x[1], y[1], 50, 50);
}

当按下 12 时更改索引 (shape_i)。按下箭头键时更改 (x[shape_i], y[shape_i]):

void keyPressed() {
  if (key == '1') {
    shape_i = 0;
  } else if (key == '2') {
    shape_i = 1;
  } else if (keyCode == UP) {
    y[shape_i] -= 10;
  } else if (keyCode == DOWN) {
    y[shape_i] += 10;
  } else if (keyCode == LEFT) {
    x[shape_i] -= 10;
  } else if (keyCode == RIGHT) {
    x[shape_i] += 10;
  }
}

完整示例:

int x[] = new int[]{100, 100};
int y[] = new int[]{200, 100};
int shape_i = 0;  
 
void setup(){
  size (400, 400);  
}
 
void draw(){
  background(80);
  rect(x[0], y[0], 25, 25);
  ellipse(x[1], y[1], 50, 50);
}
 
void keyPressed() {
  if (key == '1') {
    shape_i = 0;
  } else if (key == '2') {
    shape_i = 1;
  } else if (keyCode == UP) {
    y[shape_i] -= 10;
  } else if (keyCode == DOWN) {
    y[shape_i] += 10;
  } else if (keyCode == LEFT) {
    x[shape_i] -= 10;
  } else if (keyCode == RIGHT) {
    x[shape_i] += 10;
  }
}