setOnKeyPressed nie działa – uwagi co do kodu

0

HEJ!

  1. mam problem z:
root.setOnKeyPressed(event ->{
           if(event.getCode() == KeyCode.W) {
                rectangleSecondPlayer.setTranslateX(50);
            }
           }); " - dlaczego gdy przycisne klawisz W to nic się nie dzieje ?
  1. chętnie posłucham innych uwag co do kodu i jak go można poprawić :)

dzięki z góry!

package sample;

import javafx.animation.Animation;
import javafx.animation.PathTransition;
import javafx.animation.StrokeTransition;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.effect.Glow;
import javafx.scene.effect.Light;
import javafx.scene.effect.Lighting;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.util.Random;

import static javafx.util.Duration.seconds;

public class Main extends Application {


    @Override
    public void start(Stage primaryStage) throws Exception {
        Pane root = new Pane();
        root.setBackground(new Background(new BackgroundFill(Color.BLACK, CornerRadii.EMPTY, Insets.EMPTY)));
        animationMakeBalls(150,root);
        makeRectangle(root);
        makeMainBall(root);
        makeRectangleFirstPlayer(root);
        makeRectangleSecondPlayer(root);


        primaryStage.setTitle("AMAZING TETRIS");
        primaryStage.setScene(new Scene(root));
        primaryStage.setFullScreen(true);
        primaryStage.show();
    }

    Color BallColor []  = {Color.BLUE,Color.ROYALBLUE};
    Random random = new Random();
    private void animationMakeBalls(int i, Pane root) {
            for (int a=0;a<i;a++) {
                Circle circle = new Circle(random.nextInt(9), BallColor[random.nextInt(2)]);
                animationMakeBallsPushBallsToCorners(circle);
                if (a%2==0) {
                    animationMakeBallsLightingBalls(circle);
                }
                if (a%3==0) {
                    circle.setOpacity(random.nextInt()+0.1);
                    circle.setEffect(new Glow(1));
                }
                root.getChildren().add(circle);
            }
    }
    private void animationMakeBallsLightingBalls(Circle circle){
        Light.Point light = new Light.Point();
        light.setColor(Color.BLUE);
        light.setX(75);
        light.setY(55);
        light.setZ(45);
        Lighting lighting = new Lighting();
        lighting.setLight(light);
        circle.setEffect(lighting);
    }
    private void animationMakeBallsPushBallsToCorners(Circle circle){
        Line line = new Line();
        line.setStartX(random.nextInt(240)+680);
        line.setStartY(550);
        line.setEndX(random.nextInt(2700)-500);
        line.setEndY(random.nextInt(150)+850);

        PathTransition pathTransition = new PathTransition();
        pathTransition.setPath(line);
        pathTransition.setDuration(seconds(random.nextInt(4)+2));
        pathTransition.setCycleCount(Animation.INDEFINITE);
        pathTransition.setNode(circle);
        pathTransition.play();
    }

    private void makeRectangle(Pane root){
        Rectangle rectangle = new Rectangle(400,500);
        rectangle.setX(600);
        rectangle.setY(100);
        rectangle.setStrokeWidth(3);
        rectangle.setEffect(new Glow(1));

        StrokeTransition st = new StrokeTransition(Duration.millis(3000), rectangle, Color.DARKBLUE, Color.STEELBLUE);
        rectangle.setStrokeWidth(3);
        st.setCycleCount(Animation.INDEFINITE);
        st.setAutoReverse(true);
        st.play();


        root.getChildren().add(rectangle);

    }
    private void makeMainBall(Pane root){
        Circle circleMain = new Circle(12,Color.WHITE);
        circleMain.setTranslateX(800);
        circleMain.setTranslateY(350);
        root.getChildren().add(circleMain);
    }
    private void makeRectangleFirstPlayer(Pane root){
        Rectangle rectangleFirstPlayer = new Rectangle(90,12,Color.DARKGREEN);
        rectangleFirstPlayer.setX(755);
        rectangleFirstPlayer.setY(568);
        root.getChildren().add(rectangleFirstPlayer);
    }
    private void makeRectangleSecondPlayer(Pane root){
        Rectangle rectangleSecondPlayer = new Rectangle(90,12,Color.GOLD);
        rectangleSecondPlayer.setX(755);
        rectangleSecondPlayer.setY(120);

           root.setOnKeyPressed(event ->{
           if(event.getCode() == KeyCode.W) {
                rectangleSecondPlayer.setTranslateX(50);
            }
           });

        root.getChildren().add(rectangleSecondPlayer);
    }
    public static void main(String[] args) {
        launch(args);
    }

}

0

nikt nie wie :/ ?

1 użytkowników online, w tym zalogowanych: 0, gości: 1