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
public class Aktor extends greenfoot.Actor{}/** 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;
}