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

2019/5/30

Starting using Enter

Thotson Thotson

2019/5/30

#
1
2
3
4
5
if ((Greenfoot.isKeyDown("Enter")))
{
    getWorld().removeObject(this);
    Greenfoot.start();
}
Just wondering if there is a way to "Run" by pressing enter or another key on the keyboard. Instead of having to click the run button. Edit: So I have got this but I still have to manually press the run button.
Super_Hippo Super_Hippo

2019/5/30

#
The code is only executed when it is running. So if it isn't running, there is no way to make it start by pressing a key. However, if you put line 4 in the first world without any condition, it will start immediately. You could also have like a dummy world to start which changes to the game world when pressing enter.
Thotson Thotson

2019/5/30

#
Super_Hippo wrote...
The code is only executed when it is running. So if it isn't running, there is no way to make it start by pressing a key. However, if you put line 4 in the first world without any condition, it will start immediately. You could also have like a dummy world to start which changes to the game world when pressing enter.
makes sense, thanks.
danpost danpost

2019/5/30

#
The only way to run a scenario from a stopped state is by clicking the "Run" button. You can have your scenario start automatically and then wait for "enter" to be pressed while in a running state as the following depicts:
1
2
3
4
5
6
7
8
9
10
11
12
13
public MyWorld()
{
    super(600, 400, 1);
    Greenfoot.start();
}
 
public void act()
{
    if (Greenfoot.isKeyDown("enter"))
    {
        Greenfoot.setWorld(new Level1());
    }
}
You need to login to post a reply.