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

2013/3/14

Control a sound that actually plays with the GreenfootSound method

Solringolt Solringolt

2013/3/14

#
I got this class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class son here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class son extends menu
{
    private GreenfootImage sonOn;
    private GreenfootImage sonOff;
    private boolean Off = true;
    public GreenfootSound sound = new GreenfootSound("cliff.mp3");
    public son()
    {
        sonOn = new GreenfootImage("son1.png");
        sonOff = new GreenfootImage("son2.png");
    }
    public void act() 
    {
        if ( Greenfoot.mouseClicked(this) && Off)
        {
            setImage(sonOn);
            Greenfoot.delay(50);
            sound.playLoop();
            Off = false;
        }
        else if ( Greenfoot.mouseClicked(this) && !Off)
        {  
            setImage(sonOff);
            sound.pause();
            Off = true;
        }
    }     
}
but when you play the game i would like to stop the sound that actually plays. how can i access to the currently playing sound?
davmac davmac

2013/3/14

#
I'm not sure I understand. You have a reference to the sound in a variable called 'sound'. Just call 'sound.stop()'.
Solringolt Solringolt

2013/3/14

#
The problem is i'm not in the same world this object is on my menu. And i want to stop th sound from th gas itself
davmac davmac

2013/3/14

#
You can pass a reference to the sound to another world or object, see tutorial #6.
Solringolt Solringolt

2013/3/14

#
I really can't find it out, i don't think it's exactly the problem i have.. I try to explain more clearly I got two world: one is the menu of my game the other is the level in which my game evolves. In the menu i added a button "sound". When i click on it the sound plays, if i press again the sour go off. So I set the sound on and press on play: my level 1 is loaded with the sound still playing. When i die in the game the menu is called back. The sound is shown as off but it's still playing from before, if you click on the button you got a "double sound". So i thought i should stop playing the sound as soon as i'm dead to resolve my problem of the double sound. How can i do this?
danpost danpost

2013/3/14

#
If you are getting a double sound, then one of two things is happening. You are either playing the same sound again (on top of itself) or you are playing a seperate GreenfootSound object (of the same filename) on top of the original. Assuming that it is the first case, you should probably use the 'isPlaying' method in the menu world constructor (or its 'started' method) to determine what state to set the on/off variable for the sound, so that it will do the appropriate action when clicked on.
davmac davmac

2013/3/14

#
You are either playing the same sound again (on top of itself)
Actually, that's not possible; a single GreenfootSound can be either playing or not, but not playing multiple times. Which leaves the possibility that it's a different GreenfootSound object. Looking at the code posted above, this would only be true if you created a new instance of the 'son' class. Solringolt:
When i die in the game the menu is called back
How do you call the menu back - do you create a new menu, or do you re-use the old one? If you're creating a new one, that's the problem.
danpost danpost

2013/3/14

#
Thank you, davmac. Another lesson learned !!!
Solringolt Solringolt

2013/3/18

#
Thx for the response! I make a new menu, yes, but how could I call back the old one then?
davmac davmac

2013/3/18

#
Greenfoot.setWorld(oldMenuObject);
You need a reference to the old menu world, so you probably need to pass it to the other world as a parameter.
Solringolt Solringolt

2013/3/21

#
I tried to fix the problem but i really can't solve it... Here is the link to my game maybe the problem is not where I thaugt.. Check Zog to see what happen when I die for now. http://www.greenfoot.org/scenarios/7845
davmac davmac

2013/3/21

#
When I die, it doesn't return to the menu, it just continues running. That doesn't match with what you were saying?
danpost danpost

2013/3/21

#
You need a field in your Level1 world to hold the menu world
private World menu;
Then you need to create a second constructor in your Level1 world to accept the menu world object in a parameter.
public Level1(World menuWorld)
{
    this(); // calls your main constructor
    menu = menuWorld; // saves the menu world object
}
You could, instead, change your current constructor by adding the parameter and the saving of the menu world object to it. To use this constructor, in the jouer class,
 // change the following line
Greenfoot.setWorld(new Level1());
// to this
Greenfoot.setWorld(new Level1(getWorld()));
Finally, to return to the menu world after your main actor dies, add the following 'act' method to the Level1 world class code
public void act()
{
    if (getObjects(Cellule.class).isEmpty()) Greenfoot.setWorld(menu);
}
Solringolt Solringolt

2013/3/21

#
I reposted it but it still don't work...
danpost danpost

2013/3/22

#
You forgot to include source code on your last posting of the scenario.
You need to login to post a reply.