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

2013/9/11

Trouble with bg music

DownKost DownKost

2013/9/11

#
Im making a game and in the main menu I put a music using another world to initializated that, but I dont know how to turn off the music when I click in start game on main menu. My code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class musica here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class musica extends World
{
    //GreenfootSound backgroundMusic = new GreenfootSound("Metallica - Battery (8-Bit).mp3");
    /**
     * Constructor for objects of class musica.
     * 
     */
    public musica()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 480, 1); 
        
    }
    
    public void act()
    {
        Greenfoot.setWorld(new menu());
        //if (backgroundMusic.isPlaying()){
           // backgroundMusic.stop();
            //Greenfoot.setWorld(new Level1());            
        //}
        //backgroundMusic.playLoop();        
        
    }
    private GreenfootSound music = new GreenfootSound("Metallica - Battery (8-Bit).mp3");   
    public void started()    
    {    
       music.playLoop();    
    }    
    
    public void stopped()    
    {    
       music.stop();    
    }   
Make sure you tell the music to stop before changing worlds. Also, you commented out any part that told the music to stop.
danpost danpost

2013/9/11

#
With the code given, once line 25 executes you will lose the reference to the GreenfootSound object (in fact, you will lose any connection to the 'musica' world that will no longer be active). You could avoid the lose by passing this world on to the next, but I really do not see any real benefit by having a separate class for music control. Better might be to put the GreenfootSound field declaration statement and the two methods in one of your other world classes. It would help to know your world class structure before offering any advice as to where it might be best to move them.
You need to login to post a reply.