user3624703
user3624703

Reputation: 57

game freeze when i run a different thread javafx

i have a problem with javaFX: when i run the new thread the game freeze loading and never continue

this is the game loop class:

public class Motor implements Runnable{
private Mapa mapa;

public Motor(Canvas canvas,Label label){
    ArrayList<Nivel> lstNiveles = new ArrayList<Nivel>();
    mapa=new Mapa(lstNiveles,canvas,label);
}

public void start(){
    System.out.println(Platform.isFxApplicationThread());
    Platform.runLater(this);
}   

public void run(){
    System.out.println(Platform.isFxApplicationThread());
    ArrayList<Nivel> lstNiveles = new ArrayList<Nivel>();
    mapa=new Mapa(lstNiveles,ControladorMapa.canvas,ControladorMapa.mapa);
    mapa.cargarMapa();
    while(true){
        try{
            mapa.rePaint();

            Thread.sleep(2000);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
}

the player class:

public class PjAnimado {    
private Image imagen;
private int sx;
private int sy;
private int sw;
private int sh;
private int posX;
private int posY;
private int widht;
private int height;
private final int sMoveX=50;
private final int sMoveY=75;
private int numImag=0;
private Canvas pantalla; 



 public PjAnimado(int posX, int posY,Canvas pantalla) {
    this.imagen = new Image(getClass().getResource("../recursos/sprite_player.png").toExternalForm());
    this.sx=0;
    this.sy=0;
    this.sw=50;
    this.sh=75;
    this.widht=60;
    this.height=60;
    this.posX = posX;
    this.posY = posY;
    this.pantalla=pantalla;
}
public void paint(){
    GraphicsContext g2d = pantalla.getGraphicsContext2D();
    g2d.drawImage(imagen, sx, sy, sw, sh,posX,posY, widht, height);
}
public void moverDerecha(){
    this.numImag++;
    if(this.numImag<7){
        this.sx=sx+sMoveX;
    }else{
        this.sx=0;
    }

    this.posX+=50;
    this.posY+=50;
}
public void MoverPj(){
    this.pantalla.addEventHandler(KeyEvent.KEY_PRESSED,new EventHandler<KeyEvent>(){

        @Override
        public void handle(KeyEvent event) {
            if(event.getCode()==KeyCode.D){
                moverDerecha(); 
            }
        }

    });

}

public int getPosX() {
    return posX;
}

public void setPosX(int posX) {
    this.posX = posX;
}

public int getPosY() {
    return posY;
}
public void setPosY(int posY) {
    this.posY = posY;
}
}

the map class:

public class Mapa {

private PjAnimado pj;
private ArrayList<Nivel> lstNiveles;
private Label mapa;

public Mapa(ArrayList<Nivel> lstNiveles,Canvas canvas,Label mapa){
    this.setLstNiveles(lstNiveles);
    pj=new PjAnimado(100,100,canvas);
    this.mapa=mapa;
}

public void cargarMapa(){
    pj.paint();
    ponerFondo();

}
public void rePaint(){
    pj.paint();
}
private void ponerFondo(){
    mapa.setGraphic(new ImageView(new Image(getClass().getResource("../recursos/Desert.jpg").toExternalForm())));
}

public PjAnimado getPj() {
    return pj;
}

public void setPj(PjAnimado pj) {
    this.pj = pj;
}

public ArrayList<Nivel> getLstNiveles() {
    return lstNiveles;
}

public void setLstNiveles(ArrayList<Nivel> lstNiveles) {
    this.lstNiveles = lstNiveles;
}
}

the controller class of the fxml: in this class i load all from fxml

public class ControladorMapa implements Initializable,EventHandler<KeyEvent>{

@FXML StackPane logo1;
@FXML public static AnchorPane contPrincipal;
@FXML public static Label mapa;
@FXML public static Canvas canvas;

public void initialize(URL arg0, ResourceBundle arg1) { 
}

and finally this is the main thread a guess:

public class test extends Application {

@Override
public void start(Stage escenario) throws Exception {
    Parent root= FXMLLoader.load(getClass().getResource("Mapa.fxml"));
    Scene scene = new Scene(root);
    escenario.setScene(scene);
    escenario.show();
    Motor motor = new Motor(ControladorMapa.canvas,ControladorMapa.mapa);
    motor.start();
}
public static void main(String[] args) {
    launch(args);
}


}

Upvotes: 2

Views: 246

Answers (1)

isnot2bad
isnot2bad

Reputation: 24444

You're blocking the FX Application thread in Motor.run:

while (true) {
    try {
        mapa.rePaint();
        Thread.sleep(2000);
    } catch(Exception e){
        e.printStackTrace();
    }
}

I don't know why you need this loop but its definitely wrong to run it by the FX Application thread.

Upvotes: 2

Related Questions