我正在尝试在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/