JavaFX:使用键盘突然移动矩形会导致矩形变形"卡住"

时间:2017-08-29 21:38:20

标签: java javafx

我正在尝试创建一个类似Pong的游戏,我已经开始实现一些关于桨的运动的代码。关于桨叶本身的运动,一切都很好。然而,当我突然改变桨叶的方向时会出现问题(更具体地说,当我立即从桨叶向上移动到向下切换时,反之亦然)。桨在原地冻结几秒钟,然后沿所需的方向前进。

我已经尝试过当我移除canvas.setOnKeyReleased(new KeyReleasedHandler());时会发生什么,但是然后拨片在不停止的情况下向同一方向移动。我想在释放钥匙后能够停止桨的运动。

以下是MainApplication:

    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;
        }
    }

}

}

这是Paddle Class:

    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);
}

}

谢谢!

1 个答案:

答案 0 :(得分:2)

如果按,例如W,然后按S,然后按顺序发布W

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

这可能不是您打算做的:即使vY=0仍被按下,它也会产生S

在此之后的某个时刻,本机键盘重复将在S上再次按下,因此您再次呼叫setvY(PADDLE_SPEED),并且拨片开始移动。考虑使用两个布尔值(moveUpmoveDown)并将其设置为truefalse,然后根据其值更新速度。 E.g。

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;
        }

    }

}