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

2022/5/2

How does ask() method stops the scenario

Roshan123 Roshan123

2022/5/2

#
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
danpost danpost

2022/5/2

#
Roshan123 wrote...
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
The ask method uses thread control, making use of the Thread class wait method, in its implementation. This is something that you cannot make use of in your own codes within greenfoot. It is possible to emulate what it does, however, without using Thread operations. If you want more information on how to accomplish this, just ask
Roshan123 Roshan123

2022/5/3

#
danpost wrote...
It is possible to emulate what it does, however, without using Thread operations. If you want more information on how to accomplish this, just ask
Yes, please tell me how am I supposed to do it
danpost wrote...
This is something that you cannot make use of in your own codes within greenfoot.
And may I know the reason, why?
danpost danpost

2022/5/3

#
Roshan123 wrote...
<< Quote Omitted >> please tell me how am I supposed to do it
I will make a scenario with an example, but it might be a few days before it is up. I have been quite busy lately.
Roshan123 Roshan123

2022/5/4

#
danpost wrote...
I will make a scenario with an example, but it might be a few days before it is up. I have been quite busy lately.
Ok. Thanks for it. Just do it whenever you are totally available
danpost danpost

2022/5/9

#
Roshan123 wrote...
do it whenever you are totally available
Okay. I came up with the following: With a new class called Aktor:
public class Aktor extends greenfoot.Actor{}
the following, though crude, does the job (in MyWorld class of size 800x600):
/** 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;
}
Roshan123 Roshan123

2022/5/10

#
WOW! That's really an amazing, especially that Greenfoot.delay() one. Just wondering how is it (delay() method) preventing the loop from being infinite. Need some explanation about line 42 & especially line 66
danpost danpost

2022/5/11

#
Roshan123 wrote...
how is it (delay() method) preventing the loop from being infinite.
It doesn't. Line 49 prevents the loop from being infinite. When the "enter" key is pressed, execution "breaks" out of the loop.
Need some explanation about line 42
The repaint method only refreshes the screen.
& especially line 66
The delay method emulates the timing between act steps. That is, there is a normal "pause" between act steps to keep them (the act steps) evenly timed. This "pause" is when the screen is normally refreshed and the keyboard and mouse actions are updated.
Roshan123 Roshan123

2022/5/12

#
danpost wrote...
It doesn't. Line 49 prevents the loop from being infinite. When the "enter" key is pressed, execution "breaks" out of the loop.
Oh, okay and thanks a lot for the explanation and all
You need to login to post a reply.