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

2011/7/5

Start Greenfoot in Code?

Locomotion15 Locomotion15

2011/7/5

#
I noticed in the API that Greenfoot has a start() method which will "Run (or resume) the execution." I was wondering how to use that in my code considering the method stop() stops all acts. This is how I wanted to use it: A pause function. When the user presses "p," stop() is called. Then when the press "p" again, start() is called. But the program isn't running, so it can't call a method. So how do I do this?
danpost danpost

2011/7/5

#
Sorry, maybe I'm confused, too! I'm looking into it. Let you know what I find out.
danpost danpost

2011/7/5

#
I think I was right: The start() method is something you can create, to be the first code to run after the user presses the 'RUN' button. The stop() method is a blank method that you can over-write; the code will be executed before the actual stopping (or pausing) of the program. OK, so I AM NOT right! I'm confusing myself! Ignore the above, thanks.
Locomotion15 Locomotion15

2011/7/5

#
Hmm... I'm still confused on how you would actually implement it so that the user would not have to use the mouse and press RUN (I am going for full keyboard functionality here). Would stop() be overwritten to "listen" for key presses, then call start() when necessary? How would I do that? Do you have a code example I can take a look at?
danpost danpost

2011/7/5

#
The start() method is not something that is programmatically called. It is code that is executed whenever the program is started or resumed. I do not think there is a way around user pressing RUN. Again, I AM NOT right! Just confusing myself! Ignore the above.
Locomotion15 Locomotion15

2011/7/5

#
So, is there a way of holding the program just after "p" is pressed to wait for it to be pressed again, just avoiding stop() all together?
DonaldDuck DonaldDuck

2011/7/5

#
In your world class, make public boolean isRunning. In the world class, also add these two methods... EDIT: as davmac pointed out, you must use .equals() to compare strings. Code modified.
public void act()
{
    checkPause();
}

private void checkPause()
{
    String keyPressed = Greenfoot.getKey();
    if(keyPressed.equals("p"))
    {
        if(isRunning)
            isRunning = false;
        else if(!isRunning)
            isRunning = true;
    }
}
Now, in the act method of all your actors, modify the code to be as following (replace the words in the square brackets appropriately)
public void act()
{
    if((([your world name here]) getWorld()).isRunning)
    {
        [your code here]
    }
}
By doing this, your actors will only act when the boolean isRunning is true.
davmac davmac

2011/7/6

#
@danpost, I think you're confusing the started()/stopped() methods (in the World class) with the start()/stop() methods in the Greenfoot class.
mjrb4 mjrb4

2011/7/6

#
The start() method is not something that is programmatically called. It is code that is executed whenever the program is started or resumed. I do not think there is a way around user pressing RUN.
Not true - you're thinking of the started() method in the World class which is executed whenever the scenario is started (similar for the stopped method.) The start() and stop() methods in Greenfoot do just that, start and stop the scenario. EDIT: Ah, Davin beat me to it! In terms of where the start method can be used, the only place I've used it is in the world's constructor, where you can call Greenfoot.start() so that the scenario starts immediately and the user doesn't need to press the start button. You can't use it for pausing as already pointed out, since the code to check for starting it again won't be running after it's paused! If you want to pause the best way is to code it within your scenario as described above.
danpost danpost

2011/7/6

#
Sorry guys, and thanks for the correction. I was confusing started()/stopped() with start()/stop() :+( It does seem logical that 'start()' only be used in the World.class. Another thing that could be done is do a nothing loop, while getKey() != "p". NOTE: see next comment for correction, thanks.
davmac davmac

2011/7/6

#
danpost wrote...
Another thing that could be done is do a nothing loop, while getKey() != "p".
That's true, but for the sake of not burning processor time unnecessarily (and consuming your laptop battery) you should insert Greenfoot.delay(1) in your while loop, something like:
while (! "p".equals(Greenfoot.getKey())) {
    Greenfoot.delay(1);
}
This will also allow "reset" to function while the loop is running. While on the topic, note that checking for (Greenfoot.getKey() != "p") isn't correct either. The "==" and "!=" operators compare object equality, not string equality. It's possible for Greenfoot.getKey() to return "p" but for it to be a different String object than the "p" you compare it to! The same problem exists in the code posted by DonaldDuck. TLDR: use Greenfoot.delay(1) in the while loop, and check string equality using the .equals() method, not "==" operator.
Locomotion15 Locomotion15

2011/7/6

#
Thank you all! I will test both of these out shortly and announce whether or not they work.
You need to login to post a reply.