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

2017/12/2

Stopping music

SuperNinja SuperNinja

2017/12/2

#
I have music playing in one of my worlds and I'm trying to have it stop when an object is either touched or is added to a world. It was easy when the start and stop commands were in the same class, but starting it in a world class and stopping it an actor class is confusing. Thanks in advance.
danpost danpost

2017/12/3

#
You probably just need to properly cast the reference to your world. For example, if your world class name is MyWorld, then:
1
MyWorld world = (MyWorld)getWorld();
will have 'world' refer to your World subclass MyWorld as a MyWorld type object. As such, you can access the music and stop it.
SuperNinja SuperNinja

2017/12/3

#
Ok well it did something because I'm getting a different error. I'm getting a null pointer exception and it tells me it's at where I try to run my stopMusic() method. Here's the code for the background class
1
2
3
4
5
6
7
8
9
10
11
public GreenfootSound battleMusic = new GreenfootSound("battle.wav");
public void playMusic()
    {
        battleMusic.playLoop();
        battleMusic.setVolume(40);
    }
     
    public void stopMusic()
    {
        battleMusic.stop();
    }
Don't know if it's worth mentioning that I run playMusic() in the act() but I don't think that's the problem. Here's the code that supposed to stop the music when the actor touches the trophy
1
2
3
4
5
6
7
8
9
10
11
12
player playerreal = new player();
    Background music = (Background)getWorld();
    public void act()
{
playerreal = (player) getOneIntersectingObject(player.class);
        if ( isTouching(player.class) )
        {
            getWorld().removeObject(this);
            music.stopMusic();
            Greenfoot.setWorld( new VictoryScreen() );
        }
}
above is only the important part of act(), the rest is just movement stuff
SuperNinja SuperNinja

2017/12/3

#
oh forgot to mention that the second code clip is from the trophy class, not the same class from the first code clip
danpost danpost

2017/12/3

#
SuperNinja wrote...
oh forgot to mention that the second code clip is from the trophy class, not the same class from the first code clip
Move line 2 to before line 8 in the second code clip (the trophy class).
SuperNinja SuperNinja

2017/12/3

#
Thank you so much! Worked beautifully. P.S. Super appreciative for all the help you've given me and everyone else on this site
You need to login to post a reply.