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

2014/8/7

Sound Not working!

CLink08 CLink08

2014/8/7

#
I would like to stop the sound from playing when it gets to Gamover(game over), but it wont stop. Here's the code for Level1 (World) and Gamover(game over)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Level1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Level1 extends World
{
     GreenfootSound myMusic = new GreenfootSound("Dragon Ball Z Budokai 3 Opening Theme _Full Version_ _HQ_ U.S Version.mp3");
    Scoreboard counter = new Scoreboard();
    
    /**
     * Constructor for objects of class Level1.
     * 
     */
public Level1()
    {   
    
        super(600, 400, 1); 
         Beginning aworld = new Beginning();
            Greenfoot.setWorld(aworld);
    }
public Level1(boolean second)
    {   
    
        super(600, 400, 1); 
        
        addObject(new Player(), 200,200);
        addObject(counter, 100, 40);
    }

    public Scoreboard getScoreboard()
    {
        return counter;
    }
public void act()
{
    if(Greenfoot.getRandomNumber(1000) <20)
        {
            Enemy e = new Enemy();
            addObject (e,
    Greenfoot.getRandomNumber(getWidth()-20)+10, -30);
        }
    
     myMusic.playLoop();
            
    
    }
   





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

/**
 * Write a description of class Gamover here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Gamover extends World
{
     GreenfootSound myMusic = new GreenfootSound("Dragon Ball Z Budokai 3 Opening Theme _Full Version_ _HQ_ U.S Version.mp3");
    /**
    /**
     * Constructor for objects of class Gamover.
     * 
     */
    public Gamover()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        if(myMusic.isPlaying())
        {
            myMusic.stop();
        }
    }
        
    public void act()
    {
       myMusic.stop();
        if (Greenfoot.mouseClicked(this))
        {
            
            Beginning aworld = new Beginning();
            Greenfoot.setWorld(aworld);
        }
      
                    
                    
}

    }
Super_Hippo Super_Hippo

2014/8/7

#
You have two different GreenfootSound objects. You have to stop the first. So you don't need line 11 in the Gameover world class. Instead, you have to access the myMusic in Level1, which is actually playing the loop.
CLink08 CLink08

2014/8/7

#
Super_Hippo wrote...
You have two different GreenfootSound objects. You have to stop the first. So you don't need line 11 in the Gameover world class. Instead, you have to access the myMusic in Level1, which is actually playing the loop.
How would I do that?
Super_Hippo Super_Hippo

2014/8/7

#
Either you make the field myMusic public and static, the you can just write 'Level1.myMusic.stop();' or you pass the world object Level1 when you create the Gameover world. Then myMusic does not have to be static. Not sure if this works with GreenfootSound, but with other variables/objects, I would do it like that.
danpost danpost

2014/8/7

#
Wherever you have this line:
Greenfoot.setWorld(new GameOver(...));
// put this line along with it
((Level1)getWorld()).myMusic.stop();
Then you can remove everything related to the sound from the Gameover class.
You need to login to post a reply.