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
danpost danpost

2020/4/27

#
danpost wrote...
import greenfoot.*;

public class Settings extends World
{
    private static boolean muted; // sound state (muted or not muted)
    
    /**  plays given sound if not muted  */
    public static void playSound(String fname)
    {
        if (!muted) Greenfoot.playSound(fname);
    }
    
    private Actor muteButton; // or Button muteButton; // button to change muted state
    
    /** constructs a new Settings world */
    public Settings()
    {
        super(800, 600, 1);
        prepare();
    }
    
    /**  prepares mute toggle button and adds to world */
    protected void prepare()
    {
        muteButton = new Actor(){}; // or new Button("Mute");
        updateMuteButttonImage();
        addObject(muteButton, 500, 500); // wherever
    }
    
    /** applies appropriate image to mute button depending on mute state */
    private void updateMuteButtonImage()
    {
        if (!muted) muteButton.setImage(<< mute image >>);
        else muteButton.setImage(<< un-mute image >>);
    }
    
    /** detects button clicks to change muted state */
    public void act()
    {
        if (Greenfoot.mouseClicked(muteButton))
        {
            muted = !muted;
            updateMuteButtonImage();
        }
    }
}
Minari Minari

2020/4/27

#
Ok again thank you so much, but just out of curiosity whats !muted and why is there there like a green box around the curly brackets and the semi colon in new Actor(){} (Line 25)
danpost danpost

2020/4/27

#
Minari wrote...
Ok again thank you so much, but just out of curiosity whats !muted and why is there there like a green box around the curly brackets and the semi colon in new Actor(){} (Line 25)
The explanation point is the NOT operator. It returns the opposite value of the boolean (without changing the value of the field itselff):
if ( ! muted )
// can also be written as
if (muted == false)
As far as "new Actor(){}", I was not expecting you do use that as I wrote it. It creates an anonymous sub-Actor object (one that is not specifically named or sub-typed). It is internally treated as an instance of a subclass of Actor without actually having a class explicitly defining it. I was hoping you would already have a class (like Button) that you could create a clickable object from.
Minari Minari

2020/4/27

#
danpost wrote...
Minari wrote...
Ok again thank you so much, but just out of curiosity whats !muted and why is there there like a green box around the curly brackets and the semi colon in new Actor(){} (Line 25)
The explanation point is the NOT operator. It returns the opposite value of the boolean (without changing the value of the field itselff):
if ( ! muted )
// can also be written as
if (muted == false)
As far as "new Actor(){}", I was not expecting you do use that as I wrote it. It creates an anonymous sub-Actor object (one that is not specifically named or sub-typed). It is internally treated as an instance of a subclass of Actor without actually having a class explicitly defining it. I was hoping you would already have a class (like Button) that you could create a clickable object from.
I mean it work :))) So to do this correctly I need to have a new subclass of Actor say, "MusicMute". And if I do can you show exactly how this should be typed because when I tried doing that it gave me some errors. That's why I used newActor(){} because it worked.
danpost danpost

2020/4/27

#
Minari wrote...
I mean it work :))) So to do this correctly I need to have a new subclass of Actor say, "MusicMute". And if I do can you show exactly how this should be typed because when I tried doing that it gave me some errors. That's why I used newActor(){} because it worked.
You do not NEED to. You can leave it as is, if you so desire. However, I would probably use a simple Actor subclass:
public class SimpleActor extends greenfoot.Actor{}
(that one line is the entire class) and then use "new SimpleActor()" instead of "new Actor(){}".
Minari Minari

2020/4/27

#
danpost wrote...
Minari wrote...
I mean it work :))) So to do this correctly I need to have a new subclass of Actor say, "MusicMute". And if I do can you show exactly how this should be typed because when I tried doing that it gave me some errors. That's why I used newActor(){} because it worked.
You do not NEED to. You can leave it as is, if you so desire. However, I would probably use a simple Actor subclass:
public class SimpleActor extends greenfoot.Actor{}
(that one line is the entire class) and then use "new SimpleActor()" instead of "new Actor(){}".
So no need for a MusicMute actor subclass?
danpost danpost

2020/4/27

#
Minari wrote...
So no need for a MusicMute actor subclass?
Nope.
You need to login to post a reply.
1
2