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

2012/2/26

Not working, but why????

TheNightStrider TheNightStrider

2012/2/26

#
setBackground("chp1.png"); while(true){if(Greenfoot.isKeyDown("enter")){break;}} setBackground("start.png"); break; i have that code in a switch statement. When it is executed, the background doesnt change at first and when enter is press, the background goes to start.png????? Is there a easy way of doing wait until? Thx in advance If i replace the loop with a Greenfoot.delay(x) it works fine?
danpost danpost

2012/2/26

#
Did you try
while (!Greenfoot.isKeyDown("enter"));
TheNightStrider TheNightStrider

2012/2/26

#
yeah i did!
davmac davmac

2012/2/26

#
The world doesn't have a chance to repaint after you set the background to "chp1.png". Also, a tight loop like that is a bad idea - it will chew up 100% of a CPU just waiting for a keypress. You could fix both issues by inserting a Greenfoot.delay(1) in the loop:
setBackground("chp1.png");
while(true){
    Greenfoot.delay(1);        
    if(Greenfoot.isKeyDown("enter")) {
        break;
    }
}
setBackground("start.png");
break;
danpost danpost

2012/2/26

#
Guess you need to provide a bit more code (at least the method that the switch is in). @davmac, (EDITED) went to check something out before posting. You chimed in at the time I was in post-ready state. @TheNightStrider, what davmac suggested (I am confident in his ability to aid and assist)!
TheNightStrider TheNightStrider

2012/2/26

#
Thanks alot davmac!!!!!!, what do u mean the world doesnt have a chace to repaint? but thanks again
davmac davmac

2012/2/26

#
what do u mean the world doesnt have a chace to repaint?
Changes to the world (including position, rotation and appearance of actors) do not get "painted" on the screen immediately - to do so would be very inefficient (i.e. slow). Instead, the world is repainted periodically (at about 60 times per second). This can only happen when your code isn't doing anything - either between act() rounds, or when Greenfoot.delay(...) is called. In your code above, you go into a tight loop which prevents the world from being repainted.
TheNightStrider TheNightStrider

2012/2/26

#
okay - thanks - doing while(!Greenfoot.isKeyDown("enter")){Greenfoot.delay(1)} does the same thing right?
Duta Duta

2012/2/26

#
@davmac What difference would replacing Greenfoot.delay(...) with repaint() make?
davmac davmac

2012/2/27

#
doing while(!Greenfoot.isKeyDown("enter")){Greenfoot.delay(1)} does the same thing right?
Yep!
What difference would replacing Greenfoot.delay(...) with repaint() make?
It would let the world repaint, but it would still be an unbounded tight loop. See my comment above.
You need to login to post a reply.