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

2018/4/23

Stopping music when player dies

Dad Dad

2018/4/23

#
So in this game I am making for my comp sci class, I have music that I want to start playing at the start of each level. to try to do this, I created a sound object
public class sound extends Actor
{
    private GreenfootSound background;
    public sound(String name)
    {
        this.background = new GreenfootSound(name);
    }
    public void play()
    {
        background.play();
    }
    
    public void stop()
    {
        background.stop();
    }
    
    public void act() 
    {
        
    }    
}
I want to figure out how to make an instance of my sound object that can be changed by multiple classes.
public class TitleScreen extends World
{
    sound sound1;
    /**
     * Constructor for objects of class TitleScreen.
     * 
     */
    public TitleScreen()
    {    
        super(30*32, 25*32, 1);
        for(int i = 0; i <= 30; i++)
        {
            addObject(new obstacle("platform2.png"), i*32, 15);
        }
        for(int i = 0; i <= 30; i++)
        {
            addObject(new obstacle(), i*32, (25*32)-15);
        }
        addObject(new tile("StartScreen.png"), 15*32, 12*32);
        Greenfoot.start();
        this.sound1 = new sound("startScreen.mp3");
    }
    public void act()
    {
         if (Greenfoot.isKeyDown("space")) 
       {
           Greenfoot.setWorld(new level1());
           sound1.play();
       }
    }
}
I instantiated it in this world, but when the unit calls this method, It switches worlds
public void die()
    {
        getWorld().removeObject(this);
        Greenfoot.setWorld(new deathScreen());
        this.isDead = true;
    }
So how could i make it so that i can still call the stop methods on the sound object, from a different world than the one it was instantiated in. If it would help, I can put the project on here aswell.
Yehuda Yehuda

2018/4/23

#
Instead of having an Actor with the 'background' field in it, you should have the field in your World subclass and then make it accessible to everything that needs it. Then, have a parameter in your second World constructor to pass this instance of GreenfootSound to the second World. I don't understand why you have to call the 'stop' method in DeathScreen, why do you not just call it in the 'die' method and then you won't need to pass the instance of GreenfootSound to another World subclass?
danpost danpost

2018/4/23

#
Dad wrote...
I have music that I want to start playing at the start of each level. to try to do this, I created a sound object << Code Omitted >> So how could i make it so that i can still call the stop methods on the sound object, from a different world than the one it was instantiated in.
That an example from my BgMusic Actor Class Demo wArcBar Class scenario. Even though it is an Actor subclass, you can make good use of it even without being put into any world.
Dad Dad

2018/4/23

#
Oh, that makes so much sense, thanks a lot.
You need to login to post a reply.