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

2016/5/12

Stopping the game

BearProject BearProject

2016/5/12

#
I am trying to make my main actor, a bear, of the game disappear after a health bar becomes empty. Although I do remove the object of the bear, I do not know how to make the bear stop its act method as soon as the gameover object appears.
 public void act() {
        if (Greenfoot.isKeyDown("up"))
        {
            setRotation(270);
            move(5);
            checkObstacle();
            consume();
        }
        if (Greenfoot.isKeyDown("down")) {
            setRotation(90);
            move(5); 
            checkObstacle();
            consume();

        }
        if (Greenfoot.isKeyDown("left")) {
            setRotation(180);
            move(5); 
            checkObstacle();
            consume();
        }
        if (Greenfoot.isKeyDown("right")) {
            setRotation(0); 
            move(5); 
            checkObstacle(); 
            consume();
        }
    }

    public void checkObstacle()
    { Actor rock = getOneIntersectingObject(Rock.class); 
        if(rock != null)
        { 
            World myWorld = getWorld();
            ExampleWorld exampleworld = (ExampleWorld)myWorld;
            HealthBar healthbar = exampleworld.getHealthBar();
            if(touchingRock == false){
                healthbar.loseHealthRock();
                touchingRock = true;
                if(healthbar.health<=0){
                    myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
                    myWorld.removeObject(this);
                }
            }
        }
        else{
            touchingRock = false;
        }
    }
SPower SPower

2016/5/12

#
The act method is only called of objects that are in the current world (i.e. the world that is being shown), so removing the object should stop the act method from being called. However, if you ever have a situation where you want to keep the object in the world, then you could use an instance variable like this:
private boolean inactive = false;
And then, when you want the object to no longer act, make it true:
inactive = true;
And then, at the top of your act method, put this:
 if (inactive) {
    return;
    // other stuff
}
danpost danpost

2016/5/12

#
In the act method, since the only difference between the four 'if' blocks is the direction facing, you can move the common code to after the if blocks:
public void act()
{
    if (Greenfoot.isKeyDown("up")) setRotation(270);
    if (Greenfoot.isKeyDown("down")) setRotation(90);
    if (Greenfoot.isKeyDown("left")) setRotation(180);
    if (Greenfoot.isKeyDown("right")) setRotation(0);
    move(5);
    consume();
    checkObstacle();
}
The other change I made was switching the order of the 'consume' and 'checkObstacle' calls. I presume that the consume method uses an Actor class method that requires the actor be in the world and it is possible that the checkObstacle method may remove the actor.
valdes valdes

2016/5/12

#
@danpost, with the change you made, I believe the Actor will keep on moving in the same direction. In the original code, the Actor only moves when a key is pressed. I do agree with you on switching the order of calls, and putting consume() and checkObstacle() outside of the if's, assuming there are other moving actors in the world.
danpost danpost

2016/5/12

#
valdes wrote...
@danpost, with the change you made, I believe the Actor will keep on moving in the same direction. In the original code, the Actor only moves when a key is pressed.
@valdes, you are so right. I did not consider the possibility of no key pressed when I changed it. Maybe the easiest way to change it to keep it consistent with the original code is to switch each occurrence of those two lines at all four places in the original code. Then, also, the following line will need to be placed between each 'if' block (before lines 9, 16 and 22):
if (getWorld() == null) return;
You need to login to post a reply.