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

2015/4/4

Problem with GreefootSound

0gener 0gener

2015/4/4

#
Hello, I have a class Sound with 3 private GreenfootSounds, what I want is when the Player changes World the current music stops and the other one starts. Here is what i have: - Sound class:
private GreenfootSound sound1 = new GreenfootSound("sound1.mp3");
    private GreenfootSound sound2 = new GreenfootSound("sound2.mp3");
    private GreenfootSound sound3 = new GreenfootSound("sound3.mp3");
    private GreenfootImage sound;
    /**
     * Constructor.
     */
    public Sound()
    {
        sound = getImage();
        sound.clear();
    }

public void act() 
    {
        // Add your action code here.
    }

public void playSound1()
    {
        sound1.playLoop();
    }
    
    public void stopSound1()
    {
        sound1.stop();
    }
    
    public void playSound2()
    {
        sound2.playLoop();
    }
    
    public void stopSound2()
    {
        sound2.stop();
    }
    
    public void playSound3()
    {
        sound3.playLoop();
    }
    
    public void stopSound3()
    {
        sound3.stop();
    }
I have in all Worlds a object of the Sound class.
addObject(new Sound(), 0, 0);
-Play class:
public void act() 
{
    clickPlay();
    playSound();
}   
    
public void clickPlay()
{
    World poke1 = new PokeWorld1();
    if(Greenfoot.mousePressed(this))
    {
        setImage(play2);
    }
    if(Greenfoot.mouseClicked(this))
    {
        Greenfoot.setWorld(poke1);
        poke1.addObject(new Player(), 170, 215);
        stopSound();
    }
}
    
public void playSound()
{
   soundList=getWorld().getObjects(Sound.class);
        
   soundList.get(0).playSound1();
}
    
public void stopSound()
{
   soundList=getWorld().getObjects(Sound.class);
        
   soundList.get(0).stopSound1();   
}
I want that, for example, when i press the Play, the current sound, which is sound1, stop and the world changes. What happens is, i press the Play, the world changes and the sound re-plays. Appreciate any help.
Super_Hippo Super_Hippo

2015/4/4

#
In the act method of the Play class, you stop the sound if you click on it. (By the way, move line 9 right before line 16, no need to create a new world every act cycle.) After calling the 'clickPlay' method, you have the 'playSound' method which will restart the sound if you just clicked it. What you want is maybe that you only start the sound once. Then you may put it in the contructor:
public Play()
{
    playSound();
}

public void act()
{
    clickPlay();
}
0gener 0gener

2015/4/4

#
I get what you are saying but I'm getting a NullPointerException error, maybe because it can't find anything in the soundList when I inicialize the Play object, what do you recommend I do?
danpost danpost

2015/4/4

#
The 'playSound' method uses the 'getWorld' method, which will always return a null value in the constructor of the class. So, with what Super_Hippo provided, do the following:
// change this line
public Play()
// to this
protected void addedToWorld(World world)
Now it will execute one time (automatically) when the actor is added into the world (when 'getWorld' will actually return a world object instead of null).
0gener 0gener

2015/4/4

#
Oh thanks it's working now
You need to login to post a reply.