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

2020/4/25

How do I create a button to mute all sound effects from another world

1
2
Minari Minari

2020/4/25

#
Hi all, I'd thought that I would create another discussion in case there are people who also have the same problem as me. Like in my previous discussion I am trying to make a settings menu in my Settings world where it has options such as a button that mutes sound effects from actors in my PlayWorld. I am not sure if this is even possible lol but if it is I'd appreciate if you let me know. Below is my code (there are bits that I have cut out which isn't involved in producing sound effects) Blueplayer
    public void act() 
    {
        detectRedplayerCollision();
        detectRayCollision();
        collectMoneyNote();

        if (isTouching(Home.class))
        {
            Greenfoot.playSound("fanfare.mp3");
        }

    public void detectRedplayerCollision()
    {
        if (isTouching(Redplayer.class))
        {
            Greenfoot.playSound("ouch.wav");
     }

    public void detectRayCollision()
    {
        if (isTouching(Ray.class))
        {
            Greenfoot.playSound("signal.mp3");
        }

    public void collectMoneyNote()
    {
        if (isTouching(MoneyNote.class))
        {
            removeTouching(MoneyNote.class);
            Greenfoot.playSound("chaching.mp3");
Redplayer
public void act() 

        if (isTouching(Home.class))
        {
            Greenfoot.playSound("fanfare.mp3");
        }
    {
        detectBlueplayerCollision();
        detectRayCollision();
        collectMoneyNote();
    }
    }

    public void detectBlueplayerCollision()
    {
        if (isTouching(Blueplayer.class))
        {
            Greenfoot.playSound("ouch.wav");
     }

    public void detectRayCollision()
    {
        if (isTouching(Ray.class))
        {
            Greenfoot.playSound("signal.mp3");
        }

    public void collectMoneyNote()
    {
        if (isTouching(MoneyNote.class))
        {
            removeTouching(MoneyNote.class);
            Greenfoot.playSound("chaching.mp3");
timer
    public void act() 
    {
        timeUpdate();
    }

    public void timeUpdate() 
    {
        timer--;
        getWorld().showText("Time Left : "+timer, 100,100);
        if (timer == 0) 
        {
            Greenfoot.playSound("sad.mp3");
        }
    }
Thanks in advance
RcCookie RcCookie

2020/4/25

#
So what are you trying to achive? Do you want all the sounds only to be played if there is a certain setting set to do so?
RcCookie RcCookie

2020/4/25

#
In that case I would recomend to create a settings class to do so:
public abstract class Settings{
    private static boolean muted = false;
    public static void setMute(boolean b){
        muted = b;
    }
    public static boolean muted(){
        return muted;
    }
}
In your other classes you could then do stuff like this:
RcCookie RcCookie

2020/4/25

#
if(!Settings.muted()){
    //play sound
}
Minari Minari

2020/4/25

#
RcCookie wrote...
In that case I would recomend to create a settings class to do so:
public abstract class Settings{
    private static boolean muted = false;
    public static void setMute(boolean b){
        muted = b;
    }
    public static boolean muted(){
        return muted;
    }
}
In your other classes you could then do stuff like this:
I'm trying to have a button that mutes all of the sound effects above. So just to make sure i put that code in my Settings world. Also, don't I need a actor class in Settings world with code that makes it a button so i can mute the sound effects? and do i just do this with the code below in my actor classes eg:
if (timer == 0) && if(!Settings.muted())
    {
        Greenfoot.playSound("sad.mp3");
    }
RcCookie RcCookie

2020/4/25

#
Minari wrote...
RcCookie wrote...
In that case I would recomend to create a settings class to do so:
public abstract class Settings{
    private static boolean muted = false;
    public static void setMute(boolean b){
        muted = b;
    }
    public static boolean muted(){
        return muted;
    }
}
In your other classes you could then do stuff like this:
I'm trying to have a button that mutes all of the sound effects above. So just to make sure i put that code in my Settings world. Also, don't I need a actor class in Settings world with code that makes it a button so i can mute the sound effects? and do i just do this with the code below in my actor classes eg:
if (timer == 0) && if(!Settings.muted())
    {
        Greenfoot.playSound("sad.mp3");
    }
If you want multiple conditions to be true for an if-statement to be executed, you have to write it in this style:
if(condition1 && condition2){
    //something
}
In your case that means it is this:
if(timer == 0 && !Settings.muted()){
    Greenfoot.playSound("sad.mp3");
}
Settings does not have to extend Actor as it has not got to be added to the world. In fact, due to it being abstract in the way I wrote above, you actually can't create an object from it. Therefore, all the methods are static. This means that there is basically only exactly one instance of Settings.
danpost danpost

2020/4/25

#
You cannot && if statements as your line 1 does (&& is a boolean operator -- a boolean value must be present directly on both sides of the operator). All if's must be followed by a stand-alone command or a command block:
// this (using imbedded blocks)
if (timer == 0)
{
    if (!Settings.muted())
    {
        Greenfoot.playSound("sad.mp3");
    }
}
// or this (stand-alone in a block
if (timer == 0)
{
    if (!Settings.muted()) Greenfoot.playSound("sad.mp3");
}
// or this (block in a stand-alone)
if (timer == 0) if (!Settings.muted())
{
    Greenfoot.playSound("sad.mp3");
}
// or this (single block w/concatenated conditions)
if (timer == 0 && !Settings.muted())
{
    Greenfoot.playSound("sad.mp3");
}
// or this (imbedded stand-alones)
if (timer == 0) if (!Settings.muted()) Greenfoot.playSound("sad.mp3");
// or this (single stand-alone w/concatenated conditions)
if (timer == 0 && !Settings.muted()) Greenfoot.playSound("sad.mp3");
Minari Minari

2020/4/25

#
I'm still confused as to what I should do first so if someone can tell me which step I should take first I would appreciate that. Just in case there is any confusion as to what im trying to achieve: I'm trying to have a button (which is probably going to be an actor) in my Settings world. I want that button in my Settings world to be able to mute all of the sound effects that I have made for Blueplayer, Redplayer and timer in my PlayWorld world. Settings world and PlayWorld and two separate worlds
danpost danpost

2020/4/26

#
In order to mute all sounds, it might be best to have all play sound commands channeled through a single class -- the one with the boolean muted. So, instead of Greenfoot.<< sound command >>, you would (maybe) use Settings.<< sound command >>. As an example, your new playSound method might be:
public static void playSound(String fname)
{
    if (!muted) greenfoot.Greenfoot.playSound(fname);
}
RcCookie RcCookie

2020/4/26

#
danpost wrote...
In order to mute all sounds, it might be best to have all play sound commands channeled through a single class -- the one with the boolean muted. So, instead of Greenfoot.<< sound command >>, you would (maybe) use Settings.<< sound command >>. As an example, your new playSound method might be:
public static void playSound(String fname)
{
    if (!muted) greenfoot.Greenfoot.playSound(fname);
}
This method then had to be in the settings class. You yould call it like this:
Settings.playSound("sad.mp3");
Minari Minari

2020/4/27

#
danpost wrote...
In order to mute all sounds, it might be best to have all play sound commands channeled through a single class -- the one with the boolean muted. So, instead of Greenfoot.<< sound command >>, you would (maybe) use Settings.<< sound command >>. As an example, your new playSound method might be:
public static void playSound(String fname)
{
    if (!muted) greenfoot.Greenfoot.playSound(fname);
}
Ok this is my code in Settings world with what RCcookie said added. Is this correct?
public class Settings extends World
{

    /**
     * Constructor for objects of class Settings.
     * 
     */
    public Settings()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {

    }

    public abstract class Settings
    {
        private static boolean muted = false;
        public static void setMute(boolean b){
            muted = b;
        }

        public static boolean muted(){
            return muted;
        }
    }
}
Also how do I incorporate this with the methods I already have
danpost wrote...
In order to mute all sounds, it might be best to have all play sound commands channeled through a single class -- the one with the boolean muted. So, instead of Greenfoot.<< sound command >>, you would (maybe) use Settings.<< sound command >>. As an example, your new playSound method might be:
public static void playSound(String fname)
{
    if (!muted) greenfoot.Greenfoot.playSound(fname);
}
Eg:
public void timeUpdate() 
{
    timer--;
    getWorld().showText("Time Left : "+timer, 100,100);
    if (timer == 0) 
    {
        Greenfoot.playSound("sad.mp3");
    }
public void act() 

        if (isTouching(Home.class))
        {
            Greenfoot.playSound("fanfare.mp3");
        }
    {
        detectBlueplayerCollision();
        detectRayCollision();
        collectMoneyNote();
    }
    public void detectBlueplayerCollision()
    {
        if (isTouching(Blueplayer.class))
        {
            Greenfoot.playSound("ouch.wav");
     }
I'm sorry if this seems really obvious to you guys because this is a little tricky for me probably because I just got into coding last week.
danpost danpost

2020/4/27

#
I do not think RcCookie intended for you to have a class inside another class. Your Setting world with muting code could simply be:
import greenfoot.*;

public class Settings extends World
{
    private static boolean muted;
    
    public static void playSound(String fname)
    {
        if (!muted) Greenfoot.playSound(fname);
    }
    
    private Actor muteButton; // or Button muteButton;
    
    public Settings()
    {
        super(800, 600, 1);
        prepare();
    }
    
    protected void prepare()
    {
        muteButton = new Actor(){}; // or new Button("Mute");
        updateMuteButttonImage();
        addObject(muteButton, 500, 500); // wherever
    }
    
    private void updateMuteButtonImage()
    {
        if (!muted) muteButton.setImage(<< mute image >>);
        else muteButton.setImage(<< un-mute image >>);
    }
    public void act()
    {
        if (Greenfoot.mouseClicked(muteButton))
        {
            muted = !muted;
            updateMuteButtonImage();
        }
    }
}
Minari Minari

2020/4/27

#
danpost wrote...
I do not think RcCookie intended for you to have a class inside another class. Your Setting world with muting code could simply be:
import greenfoot.*;

public class Settings extends World
{
    private static boolean muted;
    
    public static void playSound(String fname)
    {
        if (!muted) Greenfoot.playSound(fname);
    }
    
    private Actor muteButton; // or Button muteButton;
    
    public Settings()
    {
        super(800, 600, 1);
        prepare();
    }
    
    protected void prepare()
    {
        muteButton = new Actor(){}; // or new Button("Mute");
        updateMuteButttonImage();
        addObject(muteButton, 500, 500); // wherever
    }
    
    private void updateMuteButtonImage()
    {
        if (!muted) muteButton.setImage(<< mute image >>);
        else muteButton.setImage(<< un-mute image >>);
    }
    public void act()
    {
        if (Greenfoot.mouseClicked(muteButton))
        {
            muted = !muted;
            updateMuteButtonImage();
        }
    }
}
Since I have multiple sound effects do i need mutiple: public static void playSound(String fname) { if (!muted) Greenfoot.playSound(fname); for each sound file i have. Also what is the difference between string fname and fname
danpost danpost

2020/4/27

#
fname is the name given to the parameter declared as type String. Just change all "Greenfoot.playSound(..." to "Settings.playSound(..." in your other classes.
Minari Minari

2020/4/27

#
danpost wrote...
fname is the name given to the parameter declared as type String. Just change all "Greenfoot.playSound(..." to "Settings.playSound(..." in your other classes.
HEY it works. THANK YOU SO MUCH!!! Can you please explain what your methods do because I want to add some comments above it. Most of it I don't even know why it works
There are more replies on the next page.
1
2