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

2013/2/27

How stop sound ?

TomazVDSN TomazVDSN

2013/2/27

#
Hi, I am trying to create a sound control. ( ON and OFF ) only. would should happen is: If I click on the image it should change the image and start or stop the sound. But it is not working as expected, the stop doesn't work at all , and the sound repeat it self if I click start again. Anybody out there that could help me ? See the code below
public class SoundPlay extends Actor
{
    public GreenfootSound backgroundMusic = new GreenfootSound("K-Hole.mp3");
    public boolean soundPlaying = false;
    
    public void act() 
    {
       
      
    } 
}

public class Sound_ON extends SoundPlay
{
    
    public void act()
    {
       if(Greenfoot.mouseClicked(this)|| Greenfoot.isKeyDown("x")) 
       { 
              turnOFF();        
      
       }
    }

        public void turnOFF()
    {
        Sound_OFF sound_off = new Sound_OFF();
        getWorld().addObject(sound_off , 1042, 561);
        
        getWorld().removeObject(this);
        
        backgroundMusic.stop();
        soundPlaying=false;

    }
}
public class Sound_OFF extends SoundPlay
{

    public void act()
    {
        if(Greenfoot.mouseClicked(this)|| Greenfoot.isKeyDown("z"))
           { 
               turnON();
                                  
   
           }
      
    }
    
    
    public void turnON()
    {
        Sound_ON sound_on = new Sound_ON();
        getWorld().addObject(sound_on , 1042, 561);
        
        getWorld().removeObject(this);
       
        backgroundMusic.play();
        soundPlaying=true;

    }
Mepp Mepp

2013/2/27

#
I would pack it in one class, like this:
public class SoundPlay extends Actor
{
    public GreenfootSound backgroundMusic = new GreenfootSound("K-Hole.mp3");
    public boolean soundPlaying=false;

    public void changeStatus{
        if(soundPlaying){
            backgroundMusic.stop();
            setImage("soundoff.png");
            soundPlaying=false;
        }
        if(!soundPlaying){
            backgroundMusic.play();
            setImage("soundon.png");
            soundPlaying=true;
        }
    }
    
    public void act(){
        if(Greenfoot.mouseMoved(null)) mouseOver = Greenfoot.mouseMoved(this);
        MouseInfo mInfo=Greenfoot.getMouseInfo();
        if(mi!=null && mi.getButton()==1 && mouseOver) changeStatus();
    }
}
            
In the setImage("");, remover soundoff.png and soundon.png with the names of the pics. If you want that the sound restarts after it played to the end, replace backgroundMusic.play(); with backgroundMusic.playLoop(); And you should know, write the act-methos at last and th methods that are used in act() before it.
danpost danpost

2013/2/27

#
@Mepp, you need an 'else' at line 12, instead of 'if (!soundPlaying)', or the music will just re-start.
Mepp Mepp

2013/2/27

#
public class SoundPlay extends Actor  
{  
    public GreenfootSound backgroundMusic = new GreenfootSound("K-Hole.mp3");  
    public boolean soundPlaying=false;  
  
    public void changeStatus{  
        if(soundPlaying){  
            backgroundMusic.stop();  
            setImage("soundoff.png");  
            soundPlaying=false;  
        }  
        else{  
            backgroundMusic.play();  
            setImage("soundon.png");  
            soundPlaying=true;  
        }  
    }  
      
    public void act(){  
        if(Greenfoot.mouseMoved(null)) mouseOver = Greenfoot.mouseMoved(this);  
        MouseInfo mInfo=Greenfoot.getMouseInfo();  
        if(mInfo!=null && mInfo.getButton()==1 && mouseOver) changeStatus();  
    }  
}  
OK, corrected it, thx.
TomazVDSN TomazVDSN

2013/2/28

#
Mepp and danpost, thank you guys ... I will test and if it works as expected I will publish a simple game to kids in here.
TomazVDSN TomazVDSN

2013/2/28

#
In the line 06. public void changeStatus{ we have to put a parentesis like changeStatus(). In the line 20. mouseOver gets RED and do not compile. should I declare it as boolean ?
danpost danpost

2013/2/28

#
Yes. Add the following as the first statement in your 'act' method:
boolean mouseOver = false;
TomazVDSN TomazVDSN

2013/3/2

#
It doesn't work
danpost danpost

2013/3/2

#
Try this:
public class SoundPlay extends Actor
{
    public GreenfootSound backgroundMusic = new GreenfootSound("K-Hole.mp3");
    public boolean soundPlaying=false;

    public void changeStatus()
    {
        soundPlaying = !soundPlaying;
        if(!soundPlaying)
        {
            backgroundMusic.stop();
            setImage("soundoff.png");
        }
        else
        {
            backgroundMusic.play();
            setImage("soundon.png");
        }
    }

    public void act()
    {
        if(Greenfoot.mouseClicked(this)) changeStatus();
    }
}
TomazVDSN TomazVDSN

2013/3/2

#
For some reason backgroundMusic.stop(); is not working, I did re-install Greenfoot. I used GF debugger step-by-step and I saw the program executing stop(), but it still doesn't work.
TomazVDSN TomazVDSN

2013/3/2

#
Never Mind, I did find my way around and I did publish a version of how I did it. I apprecite any comment on backGroundMusic
You need to login to post a reply.