This site requires JavaScript, please enable it in your browser!
Greenfoot back
Peer202
Peer202 wrote ...

2017/2/14

Bug Code skips turn(); Statement

Peer202 Peer202

2017/2/14

#
Hello Guys im experiencing a weird bug, i am trying to get Keyboard input from Greenfoot.isKeyDown and then turn. The interessting thing is, that it just ignores the turn(); , but just when its in a while loop. When i put in the act method and pressed play, wich allso loops the code, it works fine. The Issue seems to appear a second time in my programming class. Is it just a mistake by me? Help would be much appreciated. Thanks
public class schiff extends Wind
{
 
    int WindW;
    int WindS;
    int SegelW = 0;
    boolean segeln = true;
    int x;
    int y;
   
    /**
     * Act - do whatever the schiff wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        WindS = getwinds();
        WindW = getwindw();
         System.out.println(3);
         while(x <= 400){
           int Rotate = getRotation();
           x = x+1;
           try{
           wait(1000);
        }
        catch(Exception e){};   
        System.out.println(1);
           if(Greenfoot.isKeyDown("left")){
               turn (-10);
                System.out.println("left");
                y = y+1;
            }
           if(Greenfoot.isKeyDown("right")){
               //turn(10);
               Rotate = getRotation();
               setRotation(Rotate+10);
               System.out.println("right");
                y = y+1;
            }   
     
            }
            System.out.println(2);
    }
    
}
davmac davmac

2017/2/14

#
You aren't repainting the world, and you aren't giving it a chance to repaint itself; the turn() is not being skipped, it's just that you can't see it taking effect. Don't use wait(1000); to delay (in fact, don't use it at all until you understand what it really does). It's not meant for that, and as you've written it won't work. Use Greenfoot.delay(1); instead, and as an added bonus the world will get repainted; or, if you must, use Thread.sleep(1000); (the world won't get repainted automatically). Use the world's "repaint()" method if you need to force a repaint. Finally, please indent your code properly. It's as simple as pressing ctrl+shift+I. :)
You need to login to post a reply.