Переход метки с помощью AnimationTimer в javafxJAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Переход метки с помощью AnimationTimer в javafx

Сообщение Anonymous »

У меня есть класс Mover, расширяющий класс AnimationTimer для перемещения Circle(ball).
Когда я запускаю программу (это игра в понг), мяч остановился на мгновение (0,5-1 секунду), а затем продолжил свое движение
Вот класс перемещения

Код: Выделить всё

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]
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Переход метки с помощью AnimationTimer в javafx
    Anonymous » » в форуме JAVA
    0 Ответы
    8 Просмотры
    Последнее сообщение Anonymous
  • Переход метки с помощью AnimationTimer в javafx
    Anonymous » » в форуме JAVA
    0 Ответы
    7 Просмотры
    Последнее сообщение Anonymous
  • Переход метки с помощью AnimationTimer в javafx
    Anonymous » » в форуме JAVA
    0 Ответы
    8 Просмотры
    Последнее сообщение Anonymous
  • Как предотвратить задержку при перемещении круга с помощью AnimationTimer в JavaFX?
    Anonymous » » в форуме JAVA
    0 Ответы
    7 Просмотры
    Последнее сообщение Anonymous
  • Как предотвратить задержку при перемещении круга с помощью AnimationTimer в JavaFX?
    Anonymous » » в форуме JAVA
    0 Ответы
    6 Просмотры
    Последнее сообщение Anonymous

Вернуться в «JAVA»