Программисты JAVA общаются здесь
Anonymous
Переход метки с помощью AnimationTimer в javafx
Сообщение
Anonymous » 05 окт 2024, 15:15
У меня есть класс Mover, расширяющий класс AnimationTimer для перемещения Cicle(ball).
Когда я запускаю программу (это игра в понг), мяч на мгновение наклоняется (0,5-1 секунда) и продолжает движение< /p>
вот класс перемещения
Код: Выделить всё
package com.anas.simplegame;
import javafx.animation.AnimationTimer;
public class Mover extends AnimationTimer {
private final Ball ball;
private String where;
private final double MaxHeight;
private final double MinHeight;
private final double MaxWidth;
public Mover(Ball ball, String where) {
super();
this.ball = ball;
this.where = where;
MaxHeight = Main.HEIGHT - ball.getRadius();
MinHeight = ball.getRadius();
MaxWidth = Main.WIDTH - ball.getRadius();
}
// Bottom-left / Bottom-right / Top-left / Top-right
@Override
public void handle(long l) {
if (where.equals("Bottom-left"))
bottomLeftMovement();
else if (where.equals("Top-left"))
topLeftMovement();
else if (where.equals("Top-right"))
topRightMovement();
else if (where.equals("Bottom-right"))
bottomRightMovement();
else {
System.out.println("Nothing");
stop();
}
}
private void bottomLeftMovement() {
ball.setTranslateY(ball.getTranslateY()+2);
ball.setPositionY(ball.getPositionY()+2);
ball.setTranslateX(ball.getTranslateX()-2);
ball.setPositionX(ball.getPositionX()-2);
if (ball.getPositionY() >= MaxHeight)
where = "Top-left";
else if (ball.getPositionX() {
countdown.stop();
dividedLabel.setVisible(true);
leftPlayer.setVisible(true);
rightPlayer.setVisible(true);
setBall();
setKeysEvents();
ballAnimation();
});
}
private void setBall() {
ball = new Ball(Main.WIDTH/2, Main.HEIGHT/2);
ball.setRadius(13);
ball.setStroke(Color.WHITE);
ball.setFill(Color.WHITE);
mainPane.getChildren().add(ball);
}
private void ballAnimation() {
random = new Random();
// false for left, true for right
boolean player = random.nextBoolean();
if (!player)
moveToLeft();
else
moveToRight();
}
private void moveToLeft() {
completableFuture = new CompletableFuture();
Mover mover = new Mover(ball, "Bottom-left");
mover.start();
}
private void moveToRight() {
completableFuture = new CompletableFuture();
Mover mover = new Mover(ball, "Bottom-right");
mover.start();
}
private void setKeysEvents() {
mainPane.getScene().setOnKeyPressed(e -> {
if (e.getCode() == KeyCode.UP) {
leftPlayer.setTranslateY(leftPlayer.getTranslateY()-10);
System.out.println(leftPlayer.getTranslateY());
}
});
}
класс Ball:
Код: Выделить всё
package com.anas.simplegame;
import javafx.scene.shape.Circle;
public class Ball extends Circle {
private double positionX;
private double positionY;
public Ball(double positionX, double positionY) {
super();
this.positionX = positionX;
this.positionY = positionY;
setLayoutX(positionX);
setLayoutY(positionY);
}
public double getPositionX() {
return positionX;
}
public void setPositionX(double positionX) {
this.positionX = positionX;
}
public double getPositionY() {
return positionY;
}
public void setPositionY(double positionY) {
this.positionY = positionY;
}
}
и класс Countdown:
Код: Выделить всё
package com.anas.simplegame;
import javafx.animation.AnimationTimer;
import javafx.scene.control.Label;
public class Countdown extends AnimationTimer {
private int From;
private double opacity;
private Label label;
// Starting countdown from X to 0
public Countdown(int From, Label label) {
super();
this.label = label;
this.From = From;
this.opacity = label.getOpacity();
label.setText(String.valueOf(From));
}
@Override
public void handle(long l) {
opacity -= 0.005;
label.opacityProperty().set(opacity);
if (opacity
Подробнее здесь: [url]https://stackoverflow.com/questions/79055947/label-transition-with-animationtimer-in-javafx[/url]
1728130528
Anonymous
У меня есть класс Mover, расширяющий класс AnimationTimer для перемещения Cicle(ball). Когда я запускаю программу (это игра в понг), мяч на мгновение наклоняется (0,5-1 секунда) и продолжает движение< /p> вот класс перемещения [code]package com.anas.simplegame; import javafx.animation.AnimationTimer; public class Mover extends AnimationTimer { private final Ball ball; private String where; private final double MaxHeight; private final double MinHeight; private final double MaxWidth; public Mover(Ball ball, String where) { super(); this.ball = ball; this.where = where; MaxHeight = Main.HEIGHT - ball.getRadius(); MinHeight = ball.getRadius(); MaxWidth = Main.WIDTH - ball.getRadius(); } // Bottom-left / Bottom-right / Top-left / Top-right @Override public void handle(long l) { if (where.equals("Bottom-left")) bottomLeftMovement(); else if (where.equals("Top-left")) topLeftMovement(); else if (where.equals("Top-right")) topRightMovement(); else if (where.equals("Bottom-right")) bottomRightMovement(); else { System.out.println("Nothing"); stop(); } } private void bottomLeftMovement() { ball.setTranslateY(ball.getTranslateY()+2); ball.setPositionY(ball.getPositionY()+2); ball.setTranslateX(ball.getTranslateX()-2); ball.setPositionX(ball.getPositionX()-2); if (ball.getPositionY() >= MaxHeight) where = "Top-left"; else if (ball.getPositionX() { countdown.stop(); dividedLabel.setVisible(true); leftPlayer.setVisible(true); rightPlayer.setVisible(true); setBall(); setKeysEvents(); ballAnimation(); }); } private void setBall() { ball = new Ball(Main.WIDTH/2, Main.HEIGHT/2); ball.setRadius(13); ball.setStroke(Color.WHITE); ball.setFill(Color.WHITE); mainPane.getChildren().add(ball); } private void ballAnimation() { random = new Random(); // false for left, true for right boolean player = random.nextBoolean(); if (!player) moveToLeft(); else moveToRight(); } private void moveToLeft() { completableFuture = new CompletableFuture(); Mover mover = new Mover(ball, "Bottom-left"); mover.start(); } private void moveToRight() { completableFuture = new CompletableFuture(); Mover mover = new Mover(ball, "Bottom-right"); mover.start(); } private void setKeysEvents() { mainPane.getScene().setOnKeyPressed(e -> { if (e.getCode() == KeyCode.UP) { leftPlayer.setTranslateY(leftPlayer.getTranslateY()-10); System.out.println(leftPlayer.getTranslateY()); } }); } [/code] класс Ball: [code]package com.anas.simplegame; import javafx.scene.shape.Circle; public class Ball extends Circle { private double positionX; private double positionY; public Ball(double positionX, double positionY) { super(); this.positionX = positionX; this.positionY = positionY; setLayoutX(positionX); setLayoutY(positionY); } public double getPositionX() { return positionX; } public void setPositionX(double positionX) { this.positionX = positionX; } public double getPositionY() { return positionY; } public void setPositionY(double positionY) { this.positionY = positionY; } } [/code] и класс Countdown: [code] package com.anas.simplegame; import javafx.animation.AnimationTimer; import javafx.scene.control.Label; public class Countdown extends AnimationTimer { private int From; private double opacity; private Label label; // Starting countdown from X to 0 public Countdown(int From, Label label) { super(); this.label = label; this.From = From; this.opacity = label.getOpacity(); label.setText(String.valueOf(From)); } @Override public void handle(long l) { opacity -= 0.005; label.opacityProperty().set(opacity); if (opacity Подробнее здесь: [url]https://stackoverflow.com/questions/79055947/label-transition-with-animationtimer-in-javafx[/url]