java - Repeatable start of thread -


this code contains moveable pacman, shoots little oval after pressing space button.so boolean variable default false.it becomes true after pressing space button, , oval drawn.after pressing space button, new thread started.this thread contains code moves oval forward, , once reaches coords dissapears.so when press space first time, works fine.actually works fine after more times, elclipse keeps throwing illegalthreadstateexception.i decided put thread code while(c!=22) block, because ball dissapears when c=21, thought thread keep being runnable because while condition can't fullfilled.so point make oval moving each time press space button.this not whole code.just important part. if you'd need whole code, let me know it.thank you!!

thread:

        thread=new thread(){         public void run(){             while(c!=22){             try{             (c=0;c<=20;c++){                 newx=newx+c;                 repaint=true;                 thread.sleep(100);                   }             if(c==21){                 shoot=false;                 c=0;             }         }catch(interruptedexception v){system.out.println(v);}         }         }     }; 

paintcomponent method:

    public void paintcomponent(graphics g){     super.paintcomponent(g);      i=new imageicon("c:\\users\\jakub\\desktop\\pm.gif");     pacman=i.getimage();      g.drawimage(pacman,x,y,this);      if(!shoot)         newx=x+20;         newy=y+10;      if(shoot){     g.drawoval(newx,newy,10,10);     }      if(repaint)         repaint();  } 

key event starts thread:

if(e.getkeycode()==keyevent.vk_space){     shoot=true;     thread.start(); } 

illegalthreadstateexception thrown because call thread.start() multiple times same thread object. key avoid exception instantiate thread each time before starting (the exact implementation depends on behavior want achieve).


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -