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

2013/12/5

Starting and stopping the sound

Foppen Foppen

2013/12/5

#
I also got an Actor named SoundManager
public class SoundManager extends Actor
{
    public GreenfootSound startscreen = new GreenfootSound("startscreen.wav");
    
    
    public void act() 
    {
        // Add your action code here.
    }    
}
A subclass named StartSound
public class StartSound extends SoundManager
{
    public void act() 
    {
        startscreen.play();
    }    
}
and a class named StopSound
public class StopSound extends SoundManager
{
    public void act() 
    {
        startscreen.stop();
    }    
}
I have 3 worlds: - Startmap -- StartSound added as object - Infomap -- StopSound added as object - Level1 -- StopSound added as object When I press run, the sound startscreen.wav gets played in Startmap, but as soon as I click the button to go to a new world (Infomap or Level1) it won't stop the sound.
private void checkClick()
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();  
        if(mouse != null)  
        {  
           if (Greenfoot.mouseClicked(this) != false)
           {
               if (mouse.getButton() == 1)  
               {
                   worldnamehere worldnamehere = new worldnamehere();  
                   Greenfoot.setWorld(worldnamehere);
                }  
           }
        } 
    } 
bourne bourne

2013/12/5

#
change public GreenfootSound startscreen = new GreenfootSound("startscreen.wav"); to public static GreenfootSound startscreen = new GreenfootSound("startscreen.wav");
Foppen Foppen

2013/12/5

#
It worked, thanks! Could you perhaps explain it to me?
bourne bourne

2013/12/5

#
It looks like you have 3 StopSound objects created, one for each world. Each of these would have their own instance variables (each having different instances of the GreenfootSound. Making the GreenfootSound static means it is shared between the instances of all SoundManagers (having only one instance).
Foppen Foppen

2013/12/5

#
Alright thanks.
You need to login to post a reply.