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

2015/3/10

How can i let the game start after a specific amount of time?

CSBossmann CSBossmann

2015/3/10

#
Hi, this is my third and hopefully last question about my greenfoot game. :D I have the problem that my actors are moving and spawning directly from the beginning of the game. But I want to let them start, if I mouseclick on a button. Does anybody know how can I solve this problem? Thank you!
Super_Hippo Super_Hippo

2015/3/10

#
Create the button at the beginning. Write a method which let all other actor spawn, but don't call it yet. Either save a reference to this button and check if it is clicked, or do it in the button class.
Button b = new Button();
public WorldName()
{
    addObject(b, 100, 100);
}

public void act()
{
    if (Greenfoot.mousePressed(b)) startGame();
    //if something is here which should only be executed when the game already started, have a boolean (e.g. 'started' initialized to 'false' and set it to 'true' in the 'startGame' method and use the following line
    //if (!started) return;
    //rest of code
}

private void startGame()
{
    //create the actors
}
or
public WorldName()
{
    addObject(new Button(), 100, 100);
}

public void startGame()
{
    //create the actors
}

//in the Button class
public void act()
{
    if (Greenfoot.mousePressed(this))
    {
        ((WorldName).getWorld()).startGame());
        //either remove it from the world or have a boolean to save if you already started the game
        //(so you don't start it more than once and a lot of actors are spawned)
    }
}
You need to login to post a reply.