如何为多个键侦听器创建JavaFX场景?

时间:2019-04-10 19:17:47

标签: java javafx keylistener

所以我试图创建一个Tron mulitplayer游戏,有点像蛇。 无论如何,多个人应该能够使用一个键盘玩该游戏,但是可以使用不同的组合键来引导他们在屏幕上的角色。当前,一名玩家将使用WASD键,另一名玩家应使用箭头键。

我的问题是我已经使用FXML用Javafx Scene Builder创建了一个场景。仅在创建游戏之后,我才注意到场景构建器显然仅支持一个关键的侦听器,因此目前只有一名玩家可以控制他们的游戏角色。 有什么办法可以解决这个问题?请帮助,以便一个玩家可以使用WASD键,而另一个可以使用箭头键进行导航。 我的问题与现有解决方案有所不同,因为蛇需要属于不同的参与者。仅检查是否已按下一个键输入还是同时按下了两个键是不够的,但是它需要检查是否有正确的演奏者按下了输入,即使此时另一个演奏者也按下了他们的输入。 希望能消除一些困惑。

关键侦听器

 import java.util.ArrayList;

import javafx.scene.Scene;

public class Human extends Player implements Runnable{


int keyboardControlsLayout;
Scene scene;

public Human(int stepSize, String colour, int playerNum, Scene scene, 
           int keyboardControlsLayout) {
    super(stepSize, colour, playerNum);
    // keyboardControlsLayout(0) = WASD
    // keyboardControlsLayout(1) = ARROWS
    this.keyboardControlsLayout = keyboardControlsLayout;
    this.scene = scene;

}

@Override
public void run() {
    // TODO Auto-generated method stub

    while(this.getSnake().getAlive()) {

        if (keyboardControlsLayout == 1) {
            // use WASD
            System.out.println("wasd");

            scene.setOnKeyPressed(event -> {
                switch (event.getCode()) {
                    case W:
                        snake.setDirection(0);
                        break;
                    case S:
                        snake.setDirection(1);
                        break;
                    case A:
                        snake.setDirection(3);
                        break;
                    case D:
                        snake.setDirection(2);
                        break;
                    default:
                        break;
                }
                //System.out.println(event.getCode().toString());
            });


        } else if (keyboardControlsLayout == 0) {
            System.out.println("arrows");
            // use arrow keys
            scene.setOnKeyPressed(event -> {
                switch (event.getCode()) {
                    case UP:
                        snake.setDirection(0);
                        break;
                    case DOWN:
                        snake.setDirection(1);
                        break;
                    case LEFT:
                        snake.setDirection(3);
                        break;
                    case RIGHT:
                        snake.setDirection(2);
                        break;
                    default:
                        break;
                }
            });
        }


    }

}
 }

我在哪里创建场景:

public void newGame() {
    System.out.println("<----- NEW GAME ----->");
    game = new Game(10, scene, numHumans);
    displayController.setUpDisplay(game);

    timer = new Timeline((new KeyFrame(
            Duration.millis(80),
            event -> {
                try {
                    timerTick();
                } catch (Exception e) {
                    // TODO Auto-generated catch 
                                         block
                    e.printStackTrace();
                }
            })));
    timer.setCycleCount(Animation.INDEFINITE);

    start();
}

private void start() {
// starts game and logic
}


private void loadDisplayFXMLLoader() {
    FXMLLoader displayFXMLLoader = new 
    FXMLLoader(getClass().getResource("DisplayView.fxml"));

    try {
        scene = new Scene(displayFXMLLoader.load(), 500, 525);
    } catch (IOException e) {
        Main.outputError(e);
    }

    displayController = displayFXMLLoader.getController();

}

private void loadScene() {
    this.setScene(scene);
    this.show();

    //error Handling and closing



}

任何帮助将不胜感激!

谢谢

1 个答案:

答案 0 :(得分:-1)

我对FXML不熟悉,但是我个人使用javafx,并且效果很好。如果您想尝试一下,我会调整一个适合您需求的旧程序

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class yeet {

    private static JFrame frame;
    private static JPanel panel;

    private static int x = 300;
    private static int y = 300;
    private static int x1 = 500;
    private static int y1 = 500;

    private static boolean running = true;
    private static boolean moving = false;
    private static boolean moving1 = false;
    private static boolean up;
    private static boolean down;
    private static boolean right;
    private static boolean left;
    private static boolean up1;
    private static boolean down1;
    private static boolean right1;
    private static boolean left1;

    private static Timer refresh;
    private static Timer speed;

    private static Graphics g;

    public static void main(String[] args) {

        UI();



        ActionListener al = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                if(moving && up) y--;
                if (moving && down) y++;
                if (moving && right) x++;
                if (moving && left) x--;

                if (moving1 && up1) y1--;
                if (moving1 && down1) y1++;
                if (moving1 && right1) x1++;
                if (moving1 && left1) x1--;

                panel.repaint();

            }

        };

        ActionListener al1 = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                guy(g);
                laser(g);

            }

        };

        refresh = new Timer(0, al1);
        refresh.start();
        speed = new Timer (4, al);
        speed.start();



        while (running) {
            guy(g);
            laser(g);

        }

    }

    public static void guy(Graphics g) {
        g = panel.getGraphics();
        g.setColor(Color.green);
        g.drawRect(x, y, 25, 25);
        g.fillRect(x, y, 25, 25);

    }

    public static void laser (Graphics g) {
        g = panel.getGraphics();
        g.setColor(Color.red);
        g.drawRect(x1, y1, 30, 30);
        g.fillRect(x1, y1, 30, 30);
    }

    public static void UI () {

        frame = new JFrame("Multiple movement test");
        panel = new JPanel();

        panel.setBackground(Color.black);
        panel.setLayout(null);

        frame.setSize(600,600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.setVisible(true);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.addKeyListener(new KeyListener() {

            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_UP) {
                    moving = true;
                    up = true;

                    down = false;
                    right = false;
                    left = false;

                    refresh.start();
                    speed.start();
                }

                if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                    moving = true;
                    down = true;

                    up = false;
                    right = false;
                    left = false;

                    refresh.start();
                    speed.start();
                }

                if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                    moving = true;
                    left = true;

                    up = false;
                    down = false;
                    right = false;

                    refresh.start();
                    speed.start();
                }

                if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                    moving = true;
                    right = true;

                    left = false;
                    up = false;
                    down = false;

                    refresh.start();
                    speed.start();
                }

                if (e.getKeyCode() == KeyEvent.VK_W) {
                    moving1 = true;
                    up1 = true;

                    down1 = false;
                    right1 = false;
                    left1 = false;

                    refresh.start();
                    speed.start();
                }

                if (e.getKeyCode() == KeyEvent.VK_A) {
                    moving1 = true;
                    left1 = true;

                    up1 = false;
                    down1 = false;
                    right1 = false;


                    refresh.start();
                    speed.start();
                }

                if (e.getKeyCode() == KeyEvent.VK_S) {
                    moving1 = true;
                    down1 = true;

                    up1 = false;
                    right1 = false;
                    left1 = false;

                    refresh.start();
                    speed.start();
                }

                if (e.getKeyCode() == KeyEvent.VK_D) {
                    moving1 = true;
                    right1 = true;

                    up1 = false;
                    down1 = false;
                    left1 = false;

                    refresh.start();
                    speed.start();
                }

            }

            @Override
            public void keyReleased(KeyEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void keyTyped(KeyEvent arg0) {
                // TODO Auto-generated method stub

            }

        });

        frame.add(panel);

    }
}   

希望这会有所帮助!