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

2015/1/17

Press a button while Game is over problem?

AnthonioBanderas AnthonioBanderas

2015/1/17

#
Hi all, I made a game with a menu, and everytime an particular moving object hits the end of the map (The right side) you will die. I made a Gameover screen and obviously I want to make a the objects stop in the background when the Gameover screen is there. I used the Greenfoot.stop(); method for that, and it works, but the problem is, when I click on the 'Back to the Menu' button under the Gameover screen, it does nothing? When I click the 'Back to menu' while the player is still alive and playing it works well. I know it does have something to withGreenfoot.stop();. I tried to make the objects stop with other ways but it doesn't work like I want to. Do you guys have any simple ways to do this? This is the all code for the object, but the part it it really about is the if (getX() == 799) { part offcourse, thanks.
public class Enemy extends Actor
{
    
   
    private boolean beingDragged;
    private boolean atTargetLocation;
    
    public void act()
    {
        if (atTargetLocation) return;
        
        if (getX() == 799) {
           Greenfoot.stop();
           World myWorld = getWorld();
           GameOver gameover = new GameOver();
           myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
        }
    
        if (!beingDragged)
        {
            move(1);
            if (Greenfoot.mousePressed(this)) beingDragged = true;
        }
        else
        {
            if (Greenfoot.mouseDragged(this))
            {
                MouseInfo mouse = Greenfoot.getMouseInfo();
                if (mouse != null) setLocation(mouse.getX(), mouse.getY());
                
            }
            if (Greenfoot.mouseClicked(this) || Greenfoot.mouseDragEnded(this))
            {
                beingDragged = false;
                seeLocation();
                World myWorld = getWorld();
                CarWorld carworld = (CarWorld)myWorld;
                Counter counter = carworld.getCounter();
                counter.addScore();
            }
        }
    }
    
        private void seeLocation()
    {
        atTargetLocation =
          ( getX() >= 370 &&
            getX() <= 400 &&
            getY() >= 0 &&
            getY() <= 50 ) ||
          ( getX() >= 370 &&
            getX() <= 410 &&
            getY() >= 550 &&
            getY() <= 600 ) ||
          ( getX() >= 560 &&
            getX() <= 600 &&
            getY() >= 0 &&
            getY() <= 50 ) ||
          ( getX() >= 560 &&
            getX() <= 600 &&
            getY() >= 550 &&
            getY() <= 600 ) ||
          ( getX() >= 700 &&
            getX() <= 745 &&
            getY() >= 0 &&
            getY() <= 50 ) ||
          ( getX() >= 700 &&
            getX() <= 745 &&
            getY() >= 550 &&
            getY() <= 600 );
    }
}   
Super_Hippo Super_Hippo

2015/1/17

#
Are only enemies moving? I think I didn't really get the way the game works right now. You get points for every click on an enemy and you have to place them back until one reaches the right side? Could it be that the seeLocation method (which I don't really understand why it is made like this) is quite useless? It is only called when it was dragged to a different position and if it is on a target position, it shouldn't move. How is this supposed to be? You could do it like that:
private static boolean lost = false;

public void act()
{
    if (atTargetLocation || lost) return;
    //...
    if (getX() >= 799)
    {
       lost=true;
    //...
}
When you call Greenfoot.stop(), the scenario stops after the current act cycle.
You need to login to post a reply.