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

2020/4/8

hi i am having a problem with my current mute button

Weeb.exe Weeb.exe

2020/4/8

#
so i have made a game where there is a mute button on the top left however if you press the mute button on another screen it says "java.lang.ClassCastException: class selectCharacter cannot be cast to class Main_Menu (selectCharacter and Main_Menu are in unnamed module of loader java.net.URLClassLoader @38dacfba) at Music.act(Music.java:25) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183)" this is my current code
    private GreenfootSound backgroundMusic = new GreenfootSound("Colours.wav");
    public void unmute()
    {
        backgroundMusic.playLoop();
    }

    public void mute()
    {
        backgroundMusic.pause();
    }
then i have the next line which is from the mute button.
private GreenfootImage[] images = {new GreenfootImage("Music.png"), new GreenfootImage("Mute.png")};

    public Music()
    {
        setImage(images[0]);
    }

    public void act()
    {
        if (Greenfoot.mousePressed(this))
        {
            if (getImage() == images[0])
            {
                setImage(images[1]);
                ((Main_Menu) getWorld()).mute();
            }
            else
            {
                setImage(images[0]);
                ((Main_Menu) getWorld()).unmute();
            }
        }
    }
danpost danpost

2020/4/9

#
Your Music class is written for its instances to only be placed within a Main_Menu world. I also believe that, by the way you coded the act method, the behavior of the instance will not quite be suitable to what you wanted. To fix the current issue, lines 15 and 20 will need to have a prior determination as to which type world the instance is in using conditionals like the following (for line 15):
if (getWorld() instanceof Main_Menu) ((Main_Menu)getWorld()).mute();
else if (getWorld() instanceof selectCharacter) ((selectCharacter)getWorld()).mute();
Weeb.exe Weeb.exe

2020/4/9

#
danpost wrote...
Your Music class is written for its instances to only be placed within a Main_Menu world. I also believe that, by the way you coded the act method, the behavior of the instance will not quite be suitable to what you wanted. To fix the current issue, lines 15 and 20 will need to have a prior determination as to which type world the instance is in using conditionals like the following (for line 15):
if (getWorld() instanceof Main_Menu) ((Main_Menu)getWorld()).mute();
else if (getWorld() instanceof selectCharacter) ((selectCharacter)getWorld()).mute();
ok thank you
Weeb.exe Weeb.exe

2020/4/9

#
problem now the song loops on several different pages and i cant mute the whole music at once
danpost danpost

2020/4/9

#
Weeb.exe wrote...
problem now the song loops on several different pages and i cant mute the whole music at once
Either only create one GreenfootSound instance of "Colours.wav" or stop a previous instance before playing a new instance.
Weeb.exe Weeb.exe

2020/4/10

#
oh ok
You need to login to post a reply.