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

2012/5/28

stopping action

Matt Matt

2012/5/28

#
Hi, I am using this coding to make an actor remove on click, however when the actor appears, I want the game to not be running, then when the user clicks on this particular actor it begins going again. I thought
 Greenfoot.stop() 
woould work in pausing the game but has no effect
    public void act() 
    {
        if (Greenfoot.mouseClicked(this)){
                                   Greenfoot.stop();
                   getWorld().removeObject(this);
                   Greenfoot.start();
        }
    }
danpost danpost

2012/5/28

#
The statement 'Greenfoot.stop();' will stop the execution of the scenario; which means any keystrokes, mouse clicks, etc. will NOT be answered on by the scenario, but by the Greenfoot application itself. Please explain exactly what you want to happen. Is it that you want, when this actor gets clicked on, it is removed from the world and all action is to stop until a different actor is clicked on?
Matt Matt

2012/5/28

#
What i wanted was for an actor to appear, which when clicked on is removed. I have managed to get an actor to appear and on click remove but all the other actors in the background are still acting. I Wanted for them to stop also, until the user has clicked on the actor that is being removed on click. quite confusing to explain :)
danpost danpost

2012/5/28

#
What you need to do, I believe, is add a boolean field to the world class, call it 'waitingForClick', and set it to 'false'. Then in all your actor classes, and the world class, use it to control what gets executed and what does not. In an actor class (let us say the name of your world is 'MyWorld'):
// if all the code is to be put on hold, start your act method like this
public void act()
{
    if (((MyWorld) getWorld).waitingForClick) return;
or
// if some of the code is to execute, and some not
public void act()
{
    // code that executes all the time
    if (!((MyWorld) getWorld).waitingForClick)
    {
        // code that only executes while not 'paused'
    }
    else
    {
        // code that only executes while 'paused'
        // you may not need this 'else' part
    }
}
Set the 'waitingForClick' field to 'true' when you need to 'pause' the scenario, and reset it to 'false' when the other actor object is clicked on (do not 'pause' that code; it needs to run).
You need to login to post a reply.