I was wondering how does ask method pauses every classes unless and until it doesn't get any user input. I wanna know to how is that method made
If you use stop method, then its gonna stop every classes and you can't enter any input


1 | public class Aktor extends greenfoot.Actor{} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | /** initiates "ask" with mouse click on world */ public void act() { if (Greenfoot.mouseClicked( null )) { String answer = ask( "Hey" ); } } /** returns keyboard input from user */ public String ask(String prompt) { String output = "" ; // for the user's input (to be returned) // the background for dialog area Actor panel = new Aktor(); GreenfootImage frame = new GreenfootImage( 800 , 80 ); frame.setColor( new Color( 192 , 80 , 80 )); frame.fill(); panel.setImage(frame); // the prompt Actor text = new Aktor(); GreenfootImage txt = new GreenfootImage(prompt, 28 , Color.YELLOW, new Color( 0 , 0 , 0 , 0 )); text.setImage(txt); // the keyboard input string area Actor input = new Aktor(); GreenfootImage area = new GreenfootImage( 600 , 36 ); area.setColor(Color.DARK_GRAY); area.fill(); txt = new GreenfootImage(output, 28 , Color.WHITE, new Color( 0 , 0 , 0 , 0 )); GreenfootImage copy = new GreenfootImage(area); copy.drawImage(txt, 20 , 2 ); input.setImage(copy); // add dialog actors into world addObject(panel, 400 , 560 ); addObject(text, 400 , 540 ); addObject(input, 400 , 580 ); // show new actors repaint(); // get input do { String key = Greenfoot.getKey(); if (key == null ) continue ; if ( "enter" .equals(key)) break ; if ( "backspace" .equals(key) && output.length() > 0 ) { output = output.substring( 0 , output.length()- 1 ); } if ( "space" .equals(key)) { key = " " ; } if (key.length() == 1 && key.charAt( 0 ) > 31 && key.charAt( 0 ) < 127 ) { output = output+key; } txt = new GreenfootImage(output, 28 , Color.WHITE, Color.DARK_GRAY); copy = new GreenfootImage(area); copy.drawImage(txt, 20 , 2 ); input.setImage(copy); Greenfoot.delay( 1 ); // repaints AND updates keyboard buffer } while ( true ); // remove dialog actors from world removeObject(panel); removeObject(text); removeObject(input); // return string entered via keyboard return output; } |