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 background music from another world

Minari Minari

2020/4/25

#
Hi all, I've just started coding a few days ago so I apologize if I don't understand any terminology. So right now i'm making this game that has background music in the PlayWorld world through the Music actor that I have placed in that world specifically so I have background music just in that world (Because I have other worlds in which I don't want background music in). However I have decided that I wanted some game settings in a Settings world such as an option to mute the background music in the PlayWorld world. I had an idea where I could have a button where if it is pressed it removes the the Music actor from the PlayWorld world, hence "muting" the background music. But correct me if I'm wrong, for that to happen I need to reference the worlds which I'm not sure how to do. If there is a better way to have background music in the PlayWorld world and have an option to mute it in the Settings world please let me know. This is what I'm ultimately trying to aciehve if there is no better way to mute bg music in PlayWorld world.
1
2
3
4
5
6
7
8
9
10
11
12
public class MuteButton extends Actor
{
      public void act()
      {
       click();
      }
       
      public void click()
      {
             if (Greenfoot.mouseClicked(this))
             {
                getWorld().removeObject (Music);
} This is my code for background music in the PlayWorld world
1
2
3
4
5
6
7
8
9
10
11
12
public class Music extends Actor
{
    GreenfootSound music = new GreenfootSound("bgM.wav");
    /**
     * Act - do whatever the Music wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        music.playLoop();
    }   
}
Thanks in advance
danpost danpost

2020/4/25

#
Please refer to my BGMusic Actor Class Demo scenario.
Minari Minari

2020/4/26

#
danpost wrote...
Please refer to my BGMusic Actor Class Demo scenario.
Umm if you don't mind can you tell me the parts that i need for what i'm trying to do because I don't really understand what everything does yet so i don't know which parts i can take from your scenario and implement it for my purposes because i'm still new to coding. Sorry
danpost danpost

2020/4/26

#
You would include an unmodified BGMusic class to your scenario; plus mage for the actor (your own or the ones I used). Then all background music commands should go through this class.
Minari Minari

2020/4/27

#
danpost wrote...
You would include an unmodified BGMusic class to your scenario; plus mage for the actor (your own or the ones I used). Then all background music commands should go through this class.
Yes I kinda figured that but in BGMusic class there are a lot of images lines that I don't I would need for what I am indeding to do and I dont know which lines i should take out. Also what is a "mage for the actor" and when you say background music commands do you mean the one in my Music actor class and how do I make so it goes through that the class.
danpost danpost

2020/4/27

#
Minari wrote...
Yes I kinda figured that but in BGMusic class there are a lot of images lines that I don't I would need for what I am indeding to do and I dont know which lines i should take out.
It is a support class. It is not to be added to or taken away from. The image lines are for the button that gets clicked to toggle muting.
Also what is a "mage for the actor" and when you say background music commands do you mean the one in my Music actor class and how do I make so it goes through that the class.
Actually, it will be a replacement for your Music actor class. Your world would simply use:
1
2
3
BGMusic bgmusic = new BGMusic("bgM.wav");
addObjectt(bgmusic, 50, 50); // wherever
bgmusic.play();
Minari Minari

2020/4/27

#
danpost wrote...
Minari wrote...
Yes I kinda figured that but in BGMusic class there are a lot of images lines that I don't I would need for what I am indeding to do and I dont know which lines i should take out.
It is a support class. It is not to be added to or taken away from. The image lines are for the button that gets clicked to toggle muting.
Also what is a "mage for the actor" and when you say background music commands do you mean the one in my Music actor class and how do I make so it goes through that the class.
Actually, it will be a replacement for your Music actor class. Your world would simply use:
1
2
3
BGMusic bgmusic = new BGMusic("bgM.wav");
addObjectt(bgmusic, 50, 50); // wherever
bgmusic.play();
So do I just copy your BGMusic actor into my scenario? Also I'm kind of worried that would be plagiarism because I'm doing this for a school project.
danpost danpost

2020/4/27

#
Minari wrote...
So do I just copy your BGMusic actor into my scenario?
Yes.
Also I'm kind of worried that would be plagiarism because I'm doing this for a school project.
It is my creation and has been published as freeware. As long as you do not change the Author name, how can it be plagiarism?
Minari Minari

2020/4/27

#
danpost wrote...
Minari wrote...
So do I just copy your BGMusic actor into my scenario?
Yes.
Also I'm kind of worried that would be plagiarism because I'm doing this for a school project.
It is my creation and has been published as freeware. As long as you do not change the Author name, how can it be plagiarism?
1
2
3
4
5
6
private void prepare()
{
 BGMusic bgmusic = new BGMusic("bgM.wav");
 addObject(bgmusic, 50, 50); // wherever
 bgmusic.play();
 }
ok i did this but when I compiled it gave me the error, cannot find symbol - variable bgmusic in the line "bgmusic.play();. I assumed that I needed to change it to BGMusic.play(); but when I did that it came up with non-static method play ()cannot be referenced from a static context.
danpost danpost

2020/4/27

#
Minari wrote...
ok i did this but when I compiled it gave me the error, cannot find symbol - variable bgmusic in the line "bgmusic.play();. I assumed that I needed to change it to BGMusic.play(); but when I did that it came up with non-static method play ()cannot be referenced from a static context.
It does not make any sense to error on the line you noted. It would error on the preceding line first. Double check type of error and exactly where it occurs. Also, it is obviously being defined on line 3. So, I do not know how it could possibly say it cannot find it.
Minari Minari

2020/4/27

#
So I deleted all my code and made sure I copied and pasted everything correctly and it seems to work fine now. Thanks danpost
You need to login to post a reply.