简体   繁体   中英

Pause Event execution with Threaded Program on JavaFX

I am working on a proyect where I mandatory have to use threads I have a mini-game where the user can move the character with keys. I want to pause the game when the user press enter.The question here is: how I can stop KeyEvent execution using Threads?

this is my PaneOrganizer class

public class PaneOrganizer {
private BorderPane border;
private Pane _root;
private PajaroMensajero pajaro1;
private PajaroMensajero pajaro2;
private ImageView imagenPajaro1;
private ImageView imagenPajaro2;

public PaneOrganizer(){
    _root=new Pane();
    pajaro1=new PajaroMensajero("porygon_red.png");
    pajaro2=new PajaroMensajero("porygon_blue.png");
    imagenPajaro1=pajaro1.getImagenPajaro();
    imagenPajaro2=pajaro2.getImagenPajaro();
    Scene scene=Main.scene;
    scene.setOnKeyPressed(new ManejadorTeclas());
    _root.getChildren().addAll(imagenPajaro1,imagenPajaro2);
    imagenPajaro1.setLayoutX(Constantes.POSICION_INICIAL_PAJARO_1);
    imagenPajaro1.setLayoutY(0);
    imagenPajaro2.setLayoutX(Constantes.POSICION_INICIAL_PAJARO_2);
    imagenPajaro2.setLayoutY(0);
    setStyle();
}

public Pane getRoot(){
    return this._root;
}

public void setStyle(){
    Background b= new Background(new BackgroundFill(Color.GREY,CornerRadii.EMPTY,Insets.EMPTY));
    _root.setBackground(b);
}

private class ManejadorTeclas implements EventHandler<KeyEvent>{

    @Override
    public void handle(KeyEvent event) {

        double LIMITE_ALTURA=_root.heightProperty().doubleValue();
        double LIMITE_ANCHO=_root.widthProperty().doubleValue();

        System.out.println("p1: "+imagenPajaro1.getLayoutX());
        System.out.println("p2: "+imagenPajaro2.getLayoutX());

        if(event.getCode()==Constantes.UP_P1){
            double nuevaPosicionY= imagenPajaro1.getLayoutY()-Constantes.MOVIMIENTO_Y;
            if(nuevaPosicionY >= 0 &&
               !imagenPajaro2.getBoundsInParent().intersects(imagenPajaro1.getBoundsInParent().getMinX(),imagenPajaro1.getBoundsInParent().getMinY()-Constantes.MOVIMIENTO_Y,imagenPajaro1.getBoundsInParent().getWidth(),imagenPajaro1.getBoundsInParent().getHeight())){
                imagenPajaro1.setLayoutY(nuevaPosicionY);
            }
        }
        else if(event.getCode()==Constantes.DOWN_P1){
            double nuevaPosicionY= imagenPajaro1.getLayoutY()+Constantes.MOVIMIENTO_Y;
            if(nuevaPosicionY <= LIMITE_ALTURA - Constantes.TAMAÑO_POKEMON_Y &&
            !imagenPajaro2.getBoundsInParent().intersects(imagenPajaro1.getBoundsInParent().getMinX(),imagenPajaro1.getBoundsInParent().getMinY()+Constantes.MOVIMIENTO_Y,imagenPajaro1.getBoundsInParent().getWidth(),imagenPajaro1.getBoundsInParent().getHeight())){
                imagenPajaro1.setLayoutY(nuevaPosicionY);
            }

        }
        else if(event.getCode()==Constantes.LEFT_P1){
            double nuevaPosicionX= imagenPajaro1.getLayoutX()- Constantes.MOVIMIENTO_X;
            if(nuevaPosicionX>=0 &&
               !imagenPajaro2.getBoundsInParent().intersects(imagenPajaro1.getBoundsInParent().getMinX()-Constantes.MOVIMIENTO_X,imagenPajaro1.getBoundsInParent().getMinY(),imagenPajaro1.getBoundsInParent().getWidth(),imagenPajaro1.getBoundsInParent().getHeight()))
            {
                imagenPajaro1.setLayoutX(nuevaPosicionX);
            }
        }
        else if(event.getCode()==Constantes.RIGHT_P1){
            double nuevaPosicionX= imagenPajaro1.getLayoutX() + Constantes.MOVIMIENTO_X;
            if(nuevaPosicionX <= LIMITE_ANCHO - Constantes.TAMAÑO_POKEMON_X && 
               !imagenPajaro2.getBoundsInParent().intersects(imagenPajaro1.getBoundsInParent().getMinX()+Constantes.MOVIMIENTO_X,imagenPajaro1.getBoundsInParent().getMinY(),imagenPajaro1.getBoundsInParent().getWidth(),imagenPajaro1.getBoundsInParent().getHeight()))
            {
                imagenPajaro1.setLayoutX(nuevaPosicionX);
            }
        }
        else if(event.getCode()==Constantes.UP_P2){
            double nuevaPosicionY= imagenPajaro2.getLayoutY()-Constantes.MOVIMIENTO_Y;
            if(nuevaPosicionY >= 0 &&
               !imagenPajaro1.getBoundsInParent().intersects(imagenPajaro2.getBoundsInParent().getMinX(),imagenPajaro2.getBoundsInParent().getMinY()-Constantes.MOVIMIENTO_Y,imagenPajaro2.getBoundsInParent().getWidth(),imagenPajaro2.getBoundsInParent().getHeight())){
                imagenPajaro2.setLayoutY(nuevaPosicionY);
            }
        }
        else if(event.getCode()==Constantes.DOWN_P2){
            double nuevaPosicionY= imagenPajaro2.getLayoutY()+Constantes.MOVIMIENTO_Y;
            if(nuevaPosicionY <= LIMITE_ALTURA- Constantes.TAMAÑO_POKEMON_Y &&
               !imagenPajaro1.getBoundsInParent().intersects(imagenPajaro2.getBoundsInParent().getMinX(),imagenPajaro2.getBoundsInParent().getMinY()+Constantes.MOVIMIENTO_Y,imagenPajaro2.getBoundsInParent().getWidth(),imagenPajaro2.getBoundsInParent().getHeight())){
                imagenPajaro2.setLayoutY(nuevaPosicionY);
            }
        }
        else if(event.getCode()==Constantes.LEFT_P2){
            double nuevaPosicionX= imagenPajaro2.getLayoutX()- Constantes.MOVIMIENTO_X;
            if(nuevaPosicionX>=0 && 
               !imagenPajaro1.getBoundsInParent().intersects(imagenPajaro2.getBoundsInParent().getMinX()-Constantes.MOVIMIENTO_X,imagenPajaro2.getBoundsInParent().getMinY(),imagenPajaro2.getBoundsInParent().getWidth(),imagenPajaro2.getBoundsInParent().getHeight())){
                imagenPajaro2.setLayoutX(nuevaPosicionX);
            }
        }
        else if(event.getCode()==Constantes.RIGHT_P2){
            double nuevaPosicionX= imagenPajaro2.getLayoutX() + Constantes.MOVIMIENTO_X;
            if(nuevaPosicionX <= LIMITE_ANCHO - Constantes.TAMAÑO_POKEMON_X &&
               !imagenPajaro1.getBoundsInParent().intersects(imagenPajaro2.getBoundsInParent().getMinX()+Constantes.MOVIMIENTO_X,imagenPajaro2.getBoundsInParent().getMinY(),imagenPajaro2.getBoundsInParent().getWidth(),imagenPajaro2.getBoundsInParent().getHeight())){
                imagenPajaro2.setLayoutX(nuevaPosicionX);
            }
        }
        else if (event.getCode()==KeyCode.ENTER){

        }

   }
}
}

Other utility classes

public class PajaroMensajero {
private Image imagen;
private ImageView imagenView;

public PajaroMensajero(String nombreArchivo){
    this.imagen=new Image(nombreArchivo,Constantes.TAMAÑO_POKEMON_X,Constantes.TAMAÑO_POKEMON_Y,true,true);
    imagenView=new ImageView(this.imagen);
}

public ImageView getImagenPajaro(){
    return this.imagenView;
}
}

public class Constantes {
final static int TAMAÑO_X=600;
final static int TAMAÑO_Y=400;

final static int TAMAÑO_POKEMON_X=50;
final static int TAMAÑO_POKEMON_Y=50;

final static double MOVIMIENTO_X=30;
final static double MOVIMIENTO_Y=30;

final static int POSICION_INICIAL_PAJARO_1=TAMAÑO_X * 1/4;
final static int POSICION_INICIAL_PAJARO_2=TAMAÑO_X * 3/4;

final static String DIRECTORIO_IMAGENES="";

static KeyCode UP_P1=KeyCode.UP;
static KeyCode DOWN_P1=KeyCode.DOWN;
static KeyCode LEFT_P1=KeyCode.LEFT;
static KeyCode RIGHT_P1=KeyCode.RIGHT;

static KeyCode UP_P2=KeyCode.W;
static KeyCode DOWN_P2=KeyCode.S;
static KeyCode LEFT_P2=KeyCode.A;
static KeyCode RIGHT_P2=KeyCode.D;
}

There are different ways this can be done. I do not think I completely understand what you want but this might help with various scenarios.

public PaneOrganizer(){
    public static final AtomicBoolean paused = new AtomicBoolean(false);

then...

private class ManejadorTeclas implements EventHandler<KeyEvent>{

    @Override
    public void handle(KeyEvent event) {

        if (paused.get()) {
            //program is paused, do not process the KeyEvent
            return;
        }

So now on a separate Thread or when they ENTER KeyEvent is pressed, you can set the paused AtomicBoolean to true..... PaneOrganizer.paused.set(true); . You will also need a way to unpause it.

If you want to still process the ENTER key while paused, then just move the handling of the ENTER key before the check for pause....

private class ManejadorTeclas implements EventHandler<KeyEvent>{

    @Override
    public void handle(KeyEvent event) {

        if (event.getCode()==KeyCode.ENTER){
            //handle ENTER...
            //possibly toggle the pause state.... paused.set(!paused.get());
        }else if (paused.get()) {
            //program is paused, do not process the KeyEvent
            return;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM