java - 如何使用 if 或 while 语句将 20 个 "plate"外观对象(椭圆形)堆叠到 20 个?我的功能无法正常工作

标签 java processing

我正在尝试在java中编写一个20个板堆栈,使用按键功能堆叠一个新板,并使用鼠标单击功能使各个板一一消失到0。我不明白为什么我的函数不会'他们不会按照我的命令去做工作,请帮忙。

 // Declare global (shared) variables here
 float plate1X = 50;
 float plate1Y = 200;
 int plateCount = 20;


 // Do not write any statements here (must be inside methods)


   // Add statements to run once when program starts here.  For example:
  void setup() { 
   size(400,400);
   plate1X = 200;
   plate1Y = 50;
   background(255);
   plate1X = width/2;
   plate1Y = height-25;



 } // end of setup method


 void draw()
 {
   // Declare local variables here (new each time through)

   // Add statements to run each time screen is updated here
   ellipse(plate1X, plate1Y, 200,50);
   stroke(0);
   fill(50,100,40);
 }





   // Screen will be repainted automatically at the end of draw method
    // end of draw method

 // Add other methods here  
 void keyPressed() {
   plate1Y = -25;
   while( plate1Y < height) 
     ellipse(plate1X, plate1Y, 200,50);

     plate1Y = plate1Y - 10;
   }
   void mousePressed() {
     while( plate1Y <= -205)
     ellipse(plate1X, plate1Y, 200,50);

     plate1Y = plate1Y + 10;
   }

最佳答案

每次调用 draw() 方法时,您都需要绘制每个板,这种情况通常每秒发生 60 次。您可以在两个数组 plateX[]plateY[] 中保持每个板的协调,并用 numPlates 跟踪有多少个板。您可以在 keyPressed()mousePressed() 方法中向数组添加或减去条目,但不要在其中进行任何实际绘制。

// Declare global (shared) variables here
 float plate1X = 50;
 float plate1Y = 200;
 int plateCount = 20;

int numPlates = 1;
float plateX[] = new float[plateCount];
float plateY[] = new float[plateCount];

 // Do not write any statements here (must be inside methods)


   // Add statements to run once when program starts here.  For example:
  void setup() { 
   size(400,400);
   background(255);
   plate1X = width/2;
   plate1Y = height-25;
   plateX[0]=plate1X;
   plateY[0]=plate1Y;


 } // end of setup method


 void draw()
 {
   // Declare local variables here (new each time through)

   // Add statements to run each time screen is updated here
   for(int i=0;i<numPlates;++i)
    ellipse(plateX[i], plateY[i], 200,50);
   stroke(0);
   fill(50,100,40);
 }

   // Screen will be repainted automatically at the end of draw method
    // end of draw method

 // Add other methods here  
 void keyPressed() {
   plate1Y -= 25;
   if( plate1Y > 0) {
   plateX[numPlates]=plate1X;
   plateY[numPlates]=plate1Y;
     ++numPlates;
   }
 }   
  void mousePressed() {
     --numPlates;
     plate1Y += 25;
   }

关于java - 如何使用 if 或 while 语句将 20 个 "plate"外观对象(椭圆形)堆叠到 20 个?我的功能无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22614076/

相关文章:

java - 将 Singelton 转换为 Java Spring

processing - 进口加工?

java - asadmin 命令在 ubuntu 中不起作用

java - JSON - 遍历 JSONArray

java - 带有 JScrollPane 和原型(prototype)单元格值的 JList 包裹元素名称(用点替换而不是显示水平滚动条),为什么?

java - 线程中静态同步和非静态同步方法的行为差异

opencv - 使用opencv进行处理的UnsatisfiedLinkError

image - 图像的显性 "color"

处理 - 更改默认应用程序图标

java - 在处理 2.2.1 中打开 .oni 文件时出现 SimpleOpenNI 错误