我正在做一个乒乓球比赛。当球经过球拍时,我希望它将球的位置重置到中间,然后延迟(1000)。为了使它起作用,我有两个必须为真的布尔值,但是,它仍然在使用一个或没有布尔值为真的情况下仍然存在。为什么会这样呢? 代码:
Paddle m;
Paddle o;
Ball b;
float sizeX;
float circleX = 800/2;
float circleY;
float xSpeed;
float ySpeed = 3;
float yAcceleration = -1;
float xAcceleration = -1;
boolean reset = true;
boolean point = false;
boolean y = false;
boolean s = false;
int u = 60;
void setup() {
size(800, 600);
m = new Paddle();
o = new Paddle();
b = new Ball();
}
void draw() {
frameRate(u);
background(0);
b.startVel();
b.ballProp();
m.mPaddle();
o.oPaddle();
o.oPaddleProp();
b.yBounce();
m.mPaddleProp();
o.oChange();
b.displayScore();
m.resetW();
m.resetL();
println(circleX);
}
//seperate tab
class Ball {
float Color = (255);
float circleDimensions = 32;
int score = 0;
void ballProp() {
fill(Color);
ellipse(circleX, circleY, circleDimensions, circleDimensions);
circleX += xSpeed;
circleY += ySpeed;
}
void startVel() {
if (reset == true) {
if (point == true) {
score += 1;
}
circleX = random(300, 500);
circleY = random(32, height);
xStartVel();
yStartVel();
delay(1000);
y = true;
point = false;
reset = false;
}
}
void xStartVel() {
if (random(1) < .5) {
xSpeed = 3;
} else {
xSpeed = -3;
}
}
void yStartVel() {
if (random(1) < .5) {
ySpeed = 3;
} else {
ySpeed = -3;
}
}
void yBounce() {
if (circleY > height - 10 || circleY < 10) {
ySpeed = ySpeed * yAcceleration;
}
}
void displayScore() {
fill(0, 102, 153);
textSize(32);
text(score, 10, 30);
}
}
//seperate tab
class Paddle {
float paddleColor = 255;
float mPaddleX = 790;
float oPaddleX = 10;
float oPaddleY = 250;
float PaddleWidth = 10;
float paddleLength = 100;
float change = 2;
void mPaddle() {
fill(paddleColor);
rect(mPaddleX, mouseY, PaddleWidth, paddleLength);
}
void oPaddle() {
fill(paddleColor);
rect(oPaddleX, oPaddleY, PaddleWidth * -1, paddleLength);
}
void mPaddleProp() {
if (circleY >= mouseY && circleY <= mouseY + paddleLength && circleX <= mPaddleX && mPaddleX - 10 <= circleX) {
xSpeed = xSpeed * xAcceleration;
}
}
void oPaddleProp() {
if (circleY >= oPaddleY && circleY <= oPaddleY + paddleLength && oPaddleX <= circleX && circleX <= oPaddleX + PaddleWidth) {
xSpeed = xSpeed * xAcceleration;
}
}
void oChange() {
if (circleY < oPaddleY + (paddleLength/2)) {
oPaddleY += change * -1;
} else if (circleY > oPaddleY - (paddleLength/2)) {
oPaddleY += change;
}
}
void resetW() {
if (circleX < oPaddleX) {
point = true;
reset = true;
}
}
void resetL() {
if (circleX > mPaddleX) {
reset = true;
}
}
}
答案 0 :(得分:0)
屏幕会在draw()
的末尾进行更新,因此必须在将球拉到重置位置后才发生延迟,否则整个重新绘制都将延迟。
这是解决问题的方法:
添加新变量
boolean doDelay = false;
在draw
的开头,检查是否需要延迟
void draw() {
if(doDelay){
delay(1000);
doDelay = false;
}
[rest of code]
发生重置时,将doDelay设置为true
void startVel() {
if (reset == true) {
doDelay = true;
[rest of code]
在下一帧开始时会有延迟