java - Why doesn't my run() method correctly call from my Actionlistener -


i´m working on snake implementation , done. problem is, that,when jmenuitem starting new game clicked game freezes. here´s relevant code:

             this.newgame.addactionlistener(new actionlistener()                  {              @override              public void actionperformed(actionevent e){                  neuesspiel();              }                   });                   public void neuesspiel()              {     //the old snake game finished , playing field reseted                   if(snake!=null)                   {                       snake.beendespiel();                        for(int i=0; i<20;i++)                       {                            for(int j=0;j<10;j++)                            {                                 spielfeld[i][j] = false;                             }                       }                    }//the new snake game created below                   snake = new snake(null,null, 10,5, this);                        snake snake2 = new snake(snake,null,11,5,this);                   snake.hintermann = snake2;                   snake2.hintermann = new snake(snake2,null,12,5,this);                    snake.run();                } 

in class snake:

public void run() { game.zf.requestfocus();  while(spiellauf){ try {     thread.sleep( 120 ); }  catch ( interruptedexception e ) { } if(spiellauf) bewege(); //the snake block moved } 

i´m pretty sure, run() method problem, because if don´t call method, game doesnt freeze. furthermore method neuesspiel() should ok too, because when call in constructor works intended. don´t know how else can make function wait 120ms besides using threads. there alternative? in advance!

i don´t know how else can make function wait 120ms besides using threads.

you not using threads. executing run method in current thread.

if wish run in new thread, need :

new thread(snake).start(); 

Comments