java - Binding Keys to Move an Image -


i working on game school project , 1 step have bind keys image add/subtract x-y axis. way have done not seem work , instead of adding small amount increases x/y lot , teleports image away off of screen.

the keylistener code

public void keypressed(keyevent e) {      if (e.getkeycode() == 87) {         = true ;         if (e.getkeycode() == 65) {             left = true ;          }         if (e.getkeycode() == 68) {             right = true ;         }         if (e.getkeycode() == 83) {             down = true ;         }     }  }  @override public void keyreleased(keyevent e) {      if (e.getkeycode() == 87) {         = false ;     }     if (e.getkeycode() == 65) {         left = false;      }     if (e.getkeycode() == 68) {         right = false ;     }     if (e.getkeycode() == 83) {         down = false ;     }  } 

the game loop

public void run() {      while(running) {          //player movement         if (up) {             y++ ;         }         if (left) {             x-- ;         }         if (right) {             x++;         }         if (down) {             y-- ;         }         //player movement      } }    

graphical loop

protected void paintcomponent(graphics g) {     super.paintcomponent(g);     //graphical loop start     g.drawimage(player, x, y, null) ;             //graphical loop end     repaint(); 

the code works way it's supposed to. forgot 1 thing: while-loop moving image running lot faster seem expect run. insert thread.sleep(somedelay) make loop run slower , image should move more "userfriendly" speed.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -