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

2017/4/3

How to make the gameover screen to appear

jhc0327 jhc0327

2017/4/3

#
public void act()
    {
       boolean guess = false;
       
       if(!guess){          
           if(isDown == false && Greenfoot.isKeyDown("space"))
           {
               currSong = Random();
               Greenfoot.playSound(songs[currSong] + ".mp3");
               isDown = true;
            }
       }
       
       if(guess){
           if(isDown == true && Greenfoot.isKeyDown("space")== false)
           {
               isDown = false;
           }
       }
       
       if(String.valueOf((currSong + 1)).equals(Greenfoot.getKey()) )
       {
           if(!guess){
               isDown = true;
               scr.addScore(5);
               guess = true;
            }    
       }    
            
       if(guess){
           if(isDown == false && Greenfoot.isKeyDown("space")== true)
           {
               currSong = Random();
               Greenfoot.playSound(songs[currSong] + "mp3");
               isDown = true;
               guess = true;
           }
           if(guess = true)
           {
               isDown = false;
            } 
       }       
    }
}
I am trying to make it so that whenever I get the guess wrong, the gameover screen appears. I'm not sure where to put: Greenfoot.setWorld(new Gameover());
danpost danpost

2017/4/3

#
Looks like you need to add an 'else' block at line 29 for that.
jhc0327 jhc0327

2017/4/3

#
It still doesn't work
if(String.valueOf((currSong + 1)).equals(Greenfoot.getKey()) )
       {
           if(!guess){
               isDown = true;
               scr.addScore(5);
               guess = true;
            }    
           else{
                 Greenfoot.setWorld(new Gameover());
                }     
       }
jhc0327 jhc0327

2017/4/3

#
 if(String.valueOf((currSong + 1)).equals(Greenfoot.getKey()) )
       {
           if(!guess){
               isDown = true;
               scr.addScore(5);
               guess = true;
            }           
       } 
       else{
            Greenfoot.setWorld(new Gameover());
            } 
This isn't working either
danpost danpost

2017/4/3

#
Try replacing the snippet with
String key = Greenfoot.getKey();
if (key != null && (""+(currSong+1)).equals(key))
{
    if (!guess){
        isDown = true;
        scr.addScore(5);
        guess = true;
    }
}
else if (key != null)
{
    Greenfoot.setWorld(new GameOver());
}
jhc0327 jhc0327

2017/4/3

#
Nope that's making it go straight to Gameover screen without letting me guess the question
danpost danpost

2017/4/3

#
jhc0327 wrote...
Nope that's making it go straight to Gameover screen without letting me guess the question
A key must be pressed for it to go to the Gameover screen. Line 10 ensures that. However, you could try this (but, I do not think it will change anything:
String key = Greenfoot.getKey();
if (!guess && key != null)
{
    if ((""+(currSong+1)).equals(key))
    {
        isDown = true;
        scr.addScore(5);
        guess = true;
    }
    else
    {
        Greenfoot.setWorld(new GameOver());
    }
}
jhc0327 jhc0327

2017/4/4

#
sorry that doesn't change anything, do you have any other ideas
danpost danpost

2017/4/4

#
jhc0327 wrote...
sorry that doesn't change anything, do you have any other ideas
Will have to see more of your code to see what might be going on. Your entire World subclass as well as the Actor subclasses of whatever object you place into that world.
jhc0327 jhc0327

2017/4/4

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Game here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Game extends World
{  
    private String[] songs = {"Dive", "Stay", "SupermarketFlowers", "DontLeave", "ItAintMe", "Slide", "AintMyFault", "ScaredToBeLonely" };
    private String[] answers = {"1", "2", "3", "4", "5", "6", "7", "8"};
    private boolean isDown = false;
    private Score scr;
    private int currSong = 0;
   
    /**
     * Constructor for objects of class Game.
     * 
     */
    public Game()
    {    
        super(1000, 600, 1); 
        GreenfootImage bg = new GreenfootImage("images/Upbeat.jpg");
        bg.scale(getWidth(), getHeight());
        setBackground(bg);
        //etc.
        prepare();
        isDown = false;
        scr = new Score();
        addObject (scr, 900, 50);
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Game1 game1 = new Game1("Game");
        addObject(game1,134,108);
        game1.setLocation(204,106);
        Game2 game2 = new Game2("Press spacebar to start");
        addObject(game2,113,200);
        game2.setLocation(231,193);
        Dive dive = new Dive("1. Dive");
        addObject(dive,121,272);
        dive.setLocation(162,274);
        Stay stay = new Stay("2. Stay");
        addObject(stay,314,276);
        stay.setLocation(387,273);
        SupermarketFlowers supermarketflowers = new SupermarketFlowers("3. Supermarket Flowers");
        addObject(supermarketflowers,535,270);
        supermarketflowers.setLocation(749,273);
        supermarketflowers.setLocation(737,272);
        DontLeave dontleave = new DontLeave("4. Don't Leave");
        addObject(dontleave,116,365);
        dontleave.setLocation(233,373);
        ItAintMe itaintme = new ItAintMe("5. It Ain't Me");
        addObject(itaintme,460,371);
        itaintme.setLocation(561,372);
        Slide slide = new Slide("6. Slide");
        addObject(slide,750,383);
        slide.setLocation(820,374);
        AintMyFault aintmyfault = new AintMyFault("7. Ain't My Fault");
        addObject(aintmyfault,114,489);
        aintmyfault.setLocation(252,485);
        ScaredToBeLonely scaredtobelonely = new ScaredToBeLonely("8. Scared To Be Lonely");
        addObject(scaredtobelonely,497,490);
        scaredtobelonely.setLocation(710,484);
        Game3 game3 = new Game3("You can only guess once per song");
        addObject(game3,442,200);
        game3.setLocation(529,197);
        removeObject(game3);
        Game3 game32 = new Game3("*You can only guess once per song*");
        addObject(game32,459,201);
        game32.setLocation(533,198);
    }
   
    public int Random()
    {
         return Greenfoot.getRandomNumber(songs.length);
    }
    
    
    
    public void act()
    {
       boolean guess = false;
       if(!guess){          
           if(isDown == false && Greenfoot.isKeyDown("space"))
           {
               currSong = Random();
               Greenfoot.playSound(songs[currSong] + ".mp3");
               isDown = true;
            }
       }
       
       if(guess){
           if(isDown == true && Greenfoot.isKeyDown("space")== false)
           {
               isDown = false;
           }
       }
       
       if(String.valueOf((currSong + 1)).equals(Greenfoot.getKey()) )
       {
           if(!guess){
               isDown = true;
               scr.addScore(5);
               guess = true;
            }           
           else if(guess){
                    Greenfoot.setWorld(new Gameover());
            }
       }
       
       if(guess){
           if(isDown == false && Greenfoot.isKeyDown("space")== true)
           {
               Greenfoot.playSound(songs[currSong] + "mp3");
               isDown = true;
               guess = true;
           }
           if(guess = true)
           {
               isDown = false;
           }
       }      
    }
}
That's my World class
danpost danpost

2017/4/4

#
I think you need to simplify your code a bit. Using the 'playSound' method, for one thing, does not give you any control over the sound (it will play through without you being able to stop it). You should be creating GreenfootSound objects at the time you will be playing them and assign them to a specific field until a guess is made. A simple act method might be something like this:
// witth instance fields
private String[] songs = {"Dive", "Stay", "SupermarketFlowers", "DontLeave", "ItAintMe", "Slide", "AintMyFault", "ScaredToBeLonely" };
private String[] answers = {"1", "2", "3", "4", "5", "6", "7", "8"};
private GreenfootSound playingSong;
int currSong;

public void act()
{
    // initiating new song
    if (playingSong == null && Greenfoot.isKeyDown("space"))
    {
        currSong = Random();
        playingSong = new GreenfootSound(songs[currSong]+".mp3);
        playingSong.playLoop();
    }
    // song playing
    if (playingSong != null)
    {
        String key = Greenfoot.getKey();
        if (key != null && "12345678".indexOf(key) >= 0) // valid key?
        {
            if (key.equals(answers[currSong])) // correct key?
            {
                scr.addScore(5);
                playingSong.stop();
                playingSong = null;
            }
            else // wrong key!
            {
                playingSong.stop();
                Greenfoot.setWorld(new GameOver());
            }
        }
    }
}
You need to login to post a reply.