我正在尝试使用按键功能在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;
}
答案 0 :(得分:1)
每次调用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;
}