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

2021/11/30

Music doesn

CodingCowgirl CodingCowgirl

2021/11/30

#
Hi, there! So … I’m creating my main menu right now. I build most so far. But for some reason my if the loop doesn’t work. I want to achieve that my background music is only played when the user is in the world “MainMenu” if he pastes enter it should stop. Here’s my code:
 public MainMenu()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1920, 1080, 1); 
         GreenfootImage bg = new GreenfootImage("london-background.png");
         bg.scale(getWidth(), getHeight());
         setBackground(bg);
         mainMenu();
    }
    
    private void mainMenu() {
        Logo logo = new Logo();
        addObject(logo, 960, 540);
    }
    
    private void startMusic() {
        background.playLoop();
    }
    
    private void stopMusic() {
        background.stop();
    }
    
    private void backgroundMusic() {
        if (World.MainMenu = true) {
            startMusic();
        } else {
            stopMusic();
        }
    }
 
    public void act() {
         if(Greenfoot.isKeyDown("enter")) {
             Greenfoot.playSound("mouse-click.mp3");
             Greenfoot.setWorld(new playQuiz());
            }
    }
danpost danpost

2021/11/30

#
Add the line:
stopMusic();
in the if block detecting the "enter" key being pressed (somewhere between line 33 and 36).
CodingCowgirl CodingCowgirl

2021/11/30

#
@danpost Thank you.
You need to login to post a reply.