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

2014/1/29

remove music when changing world?

1
2
0868626 0868626

2014/1/29

#
hello i have a question i want the music to stop when changing to another world (you can change worlds when you click on an actor) ,im trying for about 3 hours now but whitout result. world code:
private GreenfootSound music = new GreenfootSound("bezem.mp3");   
    public void started()    
    {    
        music.playLoop();    
    }    

    public void stopped()    
    {    
        music.stop();    
    }   
    
do you guys know how to fix this
bourne bourne

2014/1/29

#
When you change Worlds, stopped is not invoked. It is only invoked I think when you pause the scenario.
void started() This method is called by the Greenfoot system when the execution has started. void stopped() This method is called by the Greenfoot system when the execution has stopped.
0868626 0868626

2014/1/29

#
yes i know but know i want to add the option that it also stopps when you change the world. because i want to open another world with its own music.
bourne bourne

2014/1/29

#
Well you could invoke the stopped() method yourself where you call Greenfoot.setWorld
danpost danpost

2014/1/29

#
There seems to be a general mis-conception when it comes to the 'started' and 'stopped' methods. These are only executed when the scenario is stopped and started; not when a world is activated and de-activated. What you can do to stop the music is just call the 'stopped' method in your world class before (or when) you change worlds. In the actor class:
if (Greenfoot.mouseClicked(this))
{
    ((WorldClassName)getWorld()).stopped();
    Greenfoot.setWorld(new NewWorldClassName());
}
bourne bourne

2014/1/29

#
@danpost, since stopped() is a World method, you don't need to cast it to WorldClassName
danpost danpost

2014/1/29

#
bourne wrote...
@danpost, since stopped() is a World method, you don't need to cast it to WorldClassName
Even if it is over-riden in the subclass of world? will it execute the empty method of the World class or the method in the subclass? I thought that since 'getWorld' returned a World object that the compiler would not look in the subclass for the method.
bourne bourne

2014/1/29

#
The subclass. Since it is over-riden. Since getWorld() returns type World, the returned Object is guaranteed to have an implementation of method stopped(). It is redefined in the subclass. Methods are always first checked in the immediate class type, unless the keyword super is used.
bourne bourne

2014/1/29

#
The only thing that casting does, is telling the compiler to treat the thing as a given type (so to know what valid methods can be invoked on it)
0868626 0868626

2014/1/29

#
@ danpost thanks man it works, but now when i go back to the first world the music wont start again, you know how i can fix that??
danpost danpost

2014/1/29

#
I will presume you have a reference to the first world in the second, so you could go back to it.
// in second world, when returning to first
firstWorldReferenceName.started();
Greenfoot.setWorld(firstWorldReferenceName);
danpost danpost

2014/1/29

#
@bourne, thanks for the tip. Something was learnt today.
0868626 0868626

2014/1/29

#
but where do i need to write that, i have an actor in my second world wich contains the setworld code: if (Greenfoot.mouseClicked(this)) { Greenfoot.setWorld(new Menu()); } i tried to write it there but i got an error.
bourne bourne

2014/1/29

#
@danpost, glad to contribute to your day. "I don't think much of a man who is not wiser today than he was yesterday." - Abraham Lincoln
bourne bourne

2014/1/29

#
@0868626,
getWorld().stopped();
Menu menu = new Menu();
menu.started();
Greenfoot.setWorld(menu);
There are more replies on the next page.
1
2