java - How do I stop an animation once a certain barrier has been reached? -


today's question rattling mind such. i'm creating program allow user jump , move left or right, problem whenever try jump program freezes... idea behind code simple, whenever users presses space(jump) rectangle "jumps" , if y of rectangle within height above obstacle(in case rectangle dimmensions brickx,bricky,brickw, , brickh obstacle) animation supposed stop , rectangle supposed wait next command while in place on top of obstacle. method stayonbrick called during each iteration of while loop , checks; during jump, if y within needed range, , if y sets boolean jump = true should break loop on next iteration setting y needed value. when space pressed, nothing happens, or should program freezes. thoughts?

import java.awt.*; import java.awt.event.keyevent; import java.awt.event.keylistener;  public class keytest extends core implements keylistener{     window w;     public int x,y;     boolean jump = true;     public int brickx, bricky, brickw, brickh;     public static void main(string args[]){         new keytest().run();     }      private string mess = "";      //init called init superclass     public void init(){         super.init();         window w = s.getfullscreenwindow();         w.setfocustraversalkeysenabled(false);         w.addkeylistener(this);         y = s.getheight()-15;         mess = "press esc exit";     }       @override     public void keytyped(keyevent e) {         // todo auto-generated method stub         e.consume();     }      @override     public void keypressed(keyevent e) {          // todo auto-generated method stub         int keycode = e.getkeycode();         if(keycode == keyevent.vk_escape){             stop();         }else if(keycode == keyevent.vk_space){             mess = "pressed: " + keyevent.getkeytext(keycode);             while(y>s.getheight()-200){                 stayonbrick();                 if(jump==false){break;}                 else{   try{                     y-=20;                     w.repaint();                     thread.sleep(40);                 }catch(exception jumperror){                     system.out.println("error in jumping");                     }                 }              while(y<s.getheight()-15){                 stayonbrick();                 if(jump==false){                     w.repaint();                     break;}                 else{                 try{                     y+=20;                     thread.sleep(40);                     w.repaint();                 }catch(exception jumperror){                     system.out.println("error in jumping");                         }                     }                 }             }         }          else if(keycode == e.vk_right){             if(x>=s.getwidth()-30){x=s.getwidth()-30;}                 x+=20;                 w.repaint();             }         else if(keycode == e.vk_left){             if(x<=0){x=0;}             x-=20;             w.repaint();         }          e.consume();     }       @override     public void keyreleased(keyevent e) {         // todo auto-generated method stub         int keycode = e.getkeycode();         mess = "released: " + keyevent.getkeytext(keycode);         jump = true;         e.consume();     }     @override     public synchronized void draw(graphics2d g){         brickx=0; bricky=s.getheight()-100; brickw=300; brickh=20;         window w = s.getfullscreenwindow();         g.setcolor(w.getbackground());         g.fillrect(0, 0, s.getwidth(), s.getheight());         g.setcolor(w.getforeground());         g.fillrect(x, y, 30, 15);         g.drawstring(mess, 30, 30);         g.setcolor(color.blue);         g.fillrect(brickx, bricky, brickw, brickh);       }      public void stayonbrick(){         if(y<bricky && y>bricky-30){             y=bricky-15;             jump = false;            }         else{jump = true;}     } }   

whenever try jump program freezes

i'd start having @ concurrency in swing reason problem.

i'd suggest having @ how use swing timers possible solution.

i'd recommend having @ how use key bindings solve focus related issues keylistener

and might find how make sprite jump in java? , japplet creates ball bounces , gets progressively less high in java helpful


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -