java - game freeze when i run a different thread javafx -


i have problem javafx: when run new thread game freeze loading , never continue

this 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 fxml: in class load 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 main thread 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); }   } 

you're blocking fx application thread in motor.run:

while (true) {     try {         mapa.repaint();         thread.sleep(2000);     } catch(exception e){         e.printstacktrace();     } } 

i don't know why need loop wrong run fx application thread.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -