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

2018/5/22

How to stop background music when my actor dies

mc09086 mc09086

2018/5/22

#
Hi! I have developed a game for a school project and I have a question. In "MyWorld" class i start my background music as it shows
public class MyWorld extends World
{
   Start start = new Start();       
   Title title = new Title();     
    boolean isPaused = true;
    /**
     * Prepare the world for the start of the program and creates initial
     * objects.
     */
    private void prepare()
    {
        addObject(start, getWidth()/2, 450);
        addObject(title, getWidth()/2, getHeight()/4);
        Greenfoot.start();
        
    }

 public void act() 
    {
         /**
         *  Checks if the start icon has been pushed and then removes title
         *  and icon and adds the car, the counter and starts the game
         */
        if (Greenfoot.mouseClicked(start))
        {
            Counter counter = new Counter();
            addObject(counter, 50,50);
            RedCar redcar = new RedCar(counter);
            addObject(redcar,255,483);
            GreenfootSound music = new GreenfootSound ("Music.mp3");
            music.playLoop();
            isPaused = false;
         }
    }
}
How can I make my sound stop from the actor's class? In particular from here.
 public void stopsRedCar()
    {
        if (canSee(GrayCar.class)||canSee(BlackCar.class)||canSee(Cone.class))
         {
                Greenfoot.playSound("Crash.mp3");
                GameOver gameOver = new GameOver();
                getWorld().addObject(gameOver, 300, 300);
                Greenfoot.stop();
                
         }
}
Yehuda Yehuda

2018/5/22

#
You would have to create the Sound Object outside of all methods and have it be public. Then you can access it with getWorld() and a cast to your World.
mc09086 mc09086

2018/5/22

#
So I need to create two public methods into the "MyWorld" class, on for music.play and a second one for music.stop? And what do you mean by "cast" to your World? Thank you.
danpost danpost

2018/5/22

#
mc09086 wrote...
So I need to create two public methods into the "MyWorld" class, on for music.play and a second one for music.stop? And what do you mean by "cast" to your World? Thank you.
You would only need to create public method(s) if the GreenfootSound object was private. That is, as public, the GreenfootSound object would already be accessible; otherwise, you would need public method(s) to make them accessible (either with a getter method or with action method(s) to control the sound). As a public field, an actor in the world can get a reference to the GreenfootSound object with:
World world = getWorld(); // as a World object, members of MyWorld are inaccessible
MyWorld myWorld = (MyWorld) world; // the world cast as a MyWorld object
myWorld.music.stop(); // stopping the music
which can be shortened to this:
((MyWorld)getWorld()).music.stop();
This presumes you named the GreenfootSound object in MyWorld as music:
public GreenfootSound music = new GreenfootSound("Music.mp3");
Actually, if you refrain from stopping the scenario in the actor class, you could add the following act method in the MyWorld class:
public void act()
{
    if (!getObjects(GameOver.class).isEmpty())
    {
        music.stop();
        Greenfoot.stop();
    }
}
and the music field can be made private (with no getter or action methods needed for the GreenfootSound object).
mc09086 mc09086

2018/5/22

#
That worked. Thank you very very very much!!!
You need to login to post a reply.