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

2015/5/7

Adding background music in Meny and stop.

Thrillofit Thrillofit

2015/5/7

#
Hello everyone. Well. I am almost done with my programming game which I can later on show to you guys when its done. The design is done. the algorithm is done as well. Its only the sound that I'm having trouble with right now. So basically what i'm trying to do is to have a background music in my menu. Until i'm in the game. the music should just end and until i'm pressing the "back" button which takes me back to the menu. the music should start over again. But I could only make the background sound goes in loop and every time it jumps to the menu. it starts the same song but from the beginning. which means it I do that multiply times, your ears will probably not like it. So basically what I'm trying to do. Is to make this function with background music to work. While im in the Menu (The music should go in a loop) and when i'm in the "game" it should end until I press a button that is back. So I don't really know what code to show you since I have almost 20+ classes. Im posting a picture right now and I hope you guys can help me out with this! :)
danpost danpost

2015/5/7

#
Any code pertaining to that particular music in your menu class (and anywhere else) would be appropriate for posting.
Thrillofit Thrillofit

2015/5/7

#
danpost wrote...
Any code pertaining to the music in your menu class (and anywhere else) would be appropriate for posting.
I dont have kinda much code
GreenfootSound music = new GreenfootSound("Meny.wav");
    public void act() 
    {
        music.play();
      
    }
}
Been trying to do diffrent things but without result. What I was trying to do now. Inside the option. there is this: SoundOn.class :
 GreenfootSound music = new GreenfootSound("Meny.wav");
    public void act() 
    {
        mouseClicked();
    }  
   
    public void mouseClicked()
    {
        if(Greenfoot.mouseClicked(this))
        {
            music.playLoop();
        
       if(Greenfoot.mouseClicked(SoundOff.class)){
            music.pause();
    }

}
}
 }
(And nothing inside the SoundOff class) But I cant get the Sound off to work. It doesnt pause it. :/
danpost danpost

2015/5/7

#
You cannot stop the music because the 'music' field in the SoundOn class is not the same as the 'music' in the Meny class (the music source may be the same, but you have two different instances of it -- one in the Meny class and one in the SoundOn class. Remove the one in the SoundOn class, make the one in the Meny class 'static':
public static GreenfootSound music = new GreenfootSound("Meny.wav");
and change line 11 in the SoundOn class to:
Meny.music.playLoop();
Line 13 will not work (in your SoundOn class above) to detect a click on the SoundOff object because you are not referring to an object of the SoundOff class, but to the class itself. Remove lines 13 and 14 and place the appropriate code (line lines 2 through 11 plus line 15) line the SoundOff class.
Thrillofit Thrillofit

2015/5/7

#
danpost wrote...
You cannot stop the music because the 'music' field in the SoundOn class is not the same as the 'music' in the Meny class (the music source may be the same, but you have two different instances of it -- one in the Meny class and one in the SoundOn class. Remove the one in the SoundOn class, make the one in the Meny class 'static':
public static GreenfootSound music = new GreenfootSound("Meny.wav");
and change line 11 in the SoundOn class to:
Meny.music.playLoop();
Line 13 will not work (in your SoundOn class above) to detect a click on the SoundOff object because you are not referring to an object of the SoundOff class, but to the class itself. Remove lines 13 and 14 and place the appropriate code (line lines 2 through 11 plus line 15) line the SoundOff class.
Ok, So I got it to work. But as fast as I leave the Options (Pressed the Sound off and it worked) but when turning back to meny. the music starts again. What could be the problem? The code right now in Meny:
public static GreenfootSound music = new GreenfootSound("Meny.wav"); {
        music.playLoop();
        }
        
   
}
Soundon:
     public void mouseClicked()
    {
        if(Greenfoot.mouseClicked(this))
        {
            Meny.music.playLoop();
   

}
}
SoundOff:
  if(Greenfoot.mouseClicked(this))
        {
      
            Meny.music.pause();
   

}
And I do understand the problem. If im right. As fast as I turn back to the Meny. the sound will come back just because the sound in my Meny.class says that In that class. When Meny is open. Play the song. But I need somehow lead it by. when sound is off, it should be down until I turn it again.
danpost danpost

2015/5/7

#
Please post the complete Meny class code.
Thrillofit Thrillofit

2015/5/8

#
danpost wrote...
Please post the complete Meny class code.
public class Meny extends World
{

    /**
     * Constructor for objects of class Meny.
     * 
     */
    


    
    public static GreenfootSound music = new GreenfootSound("Meny.wav"); 
    {
   
     music.playLoop();
       
}
    public Meny()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 500, 1); 
        
        addObject(new MainM(), 500, 250);
       
        addObject(new ButtonStart(), 500, 240);
        addObject(new ButtonControls(), 360, 350);
        addObject(new ButtonAbout(), 675, 350);
        addObject(new SoundOn(), 677, 130);
        addObject(new SoundOff(), 821, 134);
    }
}
danpost danpost

2015/5/8

#
Lines 13 through 17 are the problem lines (remove them). Change line 12 to simply:
public static GreenfootSound music;
Then after the super call in the constructor, add the following:
if (music == null)
{
    music = new GreenfootSound("Meny.wav");
    music.playLoop();
}
This way, it will start the first time after compiling and not on subsequent times. However, it will not start after resetting.
Thrillofit Thrillofit

2015/5/8

#
danpost wrote...
Lines 13 through 17 are the problem lines (remove them). Change line 12 to simply:
public static GreenfootSound music;
Then after the super call in the constructor, add the following:
if (music == null)
{
    music = new GreenfootSound("Meny.wav");
    music.playLoop();
}
This way, it will start the first time after compiling and not on subsequent times. However, it will not start after resetting.
Awesome! Got it to work now as it should! Thank yoooou very much danpost and I appreciate the work you do! :) Thank you once again!
You need to login to post a reply.