gpt4 book ai didi

JavaFX:用键盘突然移动矩形会导致矩形变为 "stuck"

转载 作者:行者123 更新时间:2023-11-30 02:24:39 27 4
gpt4 key购买 nike

我正在尝试创建一个类似乒乓球的游戏,并且我已经开始实现一些有关 Racket 运动的代码。桨本身的运动一切正常。然而,当我突然改变桨的方向时(更具体地说,当我立即从向上移动桨切换到向下移动桨时,反之亦然),就会出现问题。桨会在原地卡住几秒钟,然后朝所需的方向移动。

我尝试看看删除 canvas.setOnKeyReleased(new KeyReleasedHandler()); 时会发生什么,但桨会继续朝同一方向移动。我希望在松开按键后能够停止桨的移动。

这是主应用程序:

    public class Pong2App extends Application {
private static final double CANVAS_WIDTH = 900;
private static final double CANVAS_HEIGHT = 500;

private static final double ELAPSED_TIME_SPEED = 1.85;

Ball testBall;
Paddle leftPaddle;

@Override
public void start(Stage primaryStage) throws Exception {
Group root = new Group();
Scene scene = new Scene(root);
primaryStage.setScene(scene);

Canvas canvas = new Canvas(CANVAS_WIDTH, CANVAS_HEIGHT);
Bounds canvasBounds = canvas.getBoundsInLocal();
root.getChildren().add(canvas);

GraphicsContext gc = canvas.getGraphicsContext2D();

// TODO: remove testBall and leftPaddle; Gives testBall random vX and vY
testBall = new Ball(CANVAS_WIDTH / 2, CANVAS_HEIGHT / 2.5, 30, 30, gc);
Random tempRand = new Random();
testBall.setvX((tempRand.nextDouble() * 4) * (tempRand.nextBoolean() ? -1 : 1));
testBall.setvY((tempRand.nextDouble() * 4) * (tempRand.nextBoolean() ? -1 : 1));

leftPaddle = new Paddle(70, CANVAS_HEIGHT / 2.5, 10, 100, "Left", gc);

canvas.setFocusTraversable(true);
canvas.setOnKeyPressed(new KeyPressedHandler());
canvas.setOnKeyReleased(new KeyReleasedHandler());

new AnimationTimer() {

@Override
public void handle(long currentTime) {

testBall.didCollideWithWalls(canvas);

if (leftPaddle.getY() <= canvasBounds.getMinY() + 5)
leftPaddle.setY(canvasBounds.getMinY() + 5);

if (leftPaddle.getY() >= canvasBounds.getMaxY() - 105)
leftPaddle.setY(canvasBounds.getMaxY() - 105);

if (leftPaddle.didCollideWith(testBall)) {
testBall.setvX(-testBall.getvX());
testBall.setX(testBall.getX() + testBall.getvX());
}

testBall.update(ELAPSED_TIME_SPEED);

leftPaddle.update(ELAPSED_TIME_SPEED);

gc.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);

testBall.render(gc);

leftPaddle.render(gc);

}

}.start();

primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}

/**
* Handles what happens when a key is pressed. Depending on what key is pressed,
* the paddle will move up or down.
*
* @author Phillip
*
*/
private class KeyPressedHandler implements EventHandler<KeyEvent> {

@Override
public void handle(KeyEvent event) {
switch (event.getCode()) {
case W:
leftPaddle.moveUp();
break;
case S:
leftPaddle.moveDown();
break;
default:
break;
}

}

}

/**
* Handles what happens when a key is released. Depending on what key is
* released, the paddle will stop moving.
*
* @author Phillip
*
*/
private class KeyReleasedHandler implements EventHandler<KeyEvent> {

@Override
public void handle(KeyEvent event) {
switch (event.getCode()) {
case W:
case S:
leftPaddle.setvY(0);
break;
default:
break;
}
}

}

}

这是桨类:

    public class Paddle extends Sprite {
private int points;
private String name;

private static final double PADDLE_SPEED = 2.5;

public Paddle(double x, double y, double width, double height, String name, GraphicsContext gc) {
super(x, y, width, height, gc);
this.points = 0;
this.name = name;
}

@Override
public boolean didCollideWith(Sprite other) {
Ball ball = (Ball) other;
double ballCenterX = ball.getCenterX();
double ballRadius = ball.getRadius();
double ballCenterY = ball.getCenterY();

double halfWidth = this.getHalfWidth();
double halfHeight = this.getHalfHeight();
double centerX = this.getCenterX();
double centerY = this.getCenterY();

if (getName().equals("Left")) {
boolean hitXBounds = ballCenterX - ballRadius <= centerX + halfWidth;
boolean hitTopPartOfBall = ballCenterY - ballRadius <= centerY + halfHeight
&& ballCenterY - ballRadius >= centerY - halfHeight;
boolean hitBotPartOfBall = ballCenterY + ballRadius <= centerY + halfHeight
&& ballCenterY + ballRadius >= centerY - halfHeight;

return hitXBounds && (hitTopPartOfBall || hitBotPartOfBall);
}
return false;
}

@Override
public void render(GraphicsContext gc) {
gc.fillRect(getX(), getY(), getWidth(), getHeight());
}

@Override
public boolean didCollideWithWalls(Canvas canvas) {
Bounds bounds = canvas.getBoundsInLocal();

boolean atTopWall = this.getY() <= bounds.getMinY();
boolean atBotWall = this.getY() >= bounds.getMaxY();

if (atTopWall || atBotWall) {
return true;
}

return false;
}

public int getPoints() {
return points;
}

public void setPoints(int points) {
this.points = points;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public void moveUp() {
this.setvY(-PADDLE_SPEED);
}

public void moveDown() {
this.setvY(PADDLE_SPEED);
}

}

谢谢!

最佳答案

如果您按,例如W,然后按S,然后释放W,您按顺序调用,

  • setvY(-PADDLE_SPEED),
  • setvY(PADDLE_SPEED),
  • setvY(0),

这可能不是您想要做的:即使仍然按下 S,它也会导致 vY=0

此后不久, native 键盘重复将再次按下 S,因此您再次调用 setvY(PADDLE_SPEED),桨开始移动。考虑使用两个 boolean 值(moveUpmoveDown)并将它们设置为 truefalse,然后更新速度基于他们的值(value)观。例如

public class Paddle extends Sprite {

private boolean moveUp ;
private boolean moveDown ;

// ...

public void setMoveUp(boolean moveUp) {
this.moveUp = moveUp ;
updateVy();
}

public void setMoveDown(booleam moveDown) {
this.moveDown = moveDown ;
updateVy();
}

private void updateVy() {
setvY(
(moveUp ? -PADDLE_SPEED : 0) +
(moveDown ? PADDLE_SPEED : 0)
);
}

// ...
}

然后在你的处理程序中执行

private class KeyPressedHandler implements EventHandler<KeyEvent> {

@Override
public void handle(KeyEvent event) {
switch (event.getCode()) {
case W:
leftPaddle.setMoveUp(true);
break;
case S:
leftPaddle.setMoveDown(true);
break;
default:
break;
}

}

}

private class KeyReleasedHandler implements EventHandler<KeyEvent> {

@Override
public void handle(KeyEvent event) {
switch (event.getCode()) {
case W:
leftPaddle.setMoveUp(false);
break ;
case S:
leftPaddle.setMoveDown(false);
break;
default:
break;
}

}

}

关于JavaFX:用键盘突然移动矩形会导致矩形变为 "stuck",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45948695/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com