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

2012/7/17

Stopping a sound when world changes

1
2
3
gusbus123 gusbus123

2012/7/17

#
I have a sound that gets played at the begining of the program running. If the player selects the game mode (goes to a new world setting) before the sound finishes then it keeps going. How can i stop this sound from playing even though it started in a different world?
gusbus123 gusbus123

2012/7/17

#
Oh and how would u fix the little space between a sound when it is looped using the playLoop(). The sound i am using should loop almost perfectly if the loop flowed smoothly, but the loop done by greenfoot has about a half second pause between each time it loops the file. Is there a way to fix this? or do i have to construct a counter that plays the sound just in time to loop on with the other sound? Edit: UGH FAIL!!! lol i was editing the wrong file... time to recheck nup still has the little halting...
davmac davmac

2012/7/17

#
First, use the most recent version of Greenfoot (2.2.1), if you're not already. Some sound bugs have been fixed. If there are still problems, I'm afraid that they are issues with either your sound file or with Java's sound functionality, and there's not much you can do about it. Perhaps you could string multiple copies of the sound together using a sound editor and save it as a single file.
erdelf erdelf

2012/7/17

#
For the loop you could use the following:
1
if(!yourMusic.isPlayed()) yourMusic.play();
for the first problem, make the GreenfootSound static
1
World1.yourMusic.stop();
danpost danpost

2012/7/17

#
Better is just to stop it before going to the new world.
1
2
3
4
5
if (gotoNewWorld == true)
{
    yourMusic.stop();
    Greenfoot.setWorld(new NewWorld());
}
Of course, line 1 should be whatever conditions need to be met to trigger going to the new world. Also, asking if the music is playing is not neccessary, as no errors are triggered if the sound is already not playing.
gusbus123 gusbus123

2012/7/18

#
@danpost the music starts in a different class to where the world gets checked to be changed. How would i check a sound from that single class through the 2 classes that can change the worlds? and thx for the feedback guys.
gusbus123 gusbus123

2012/7/18

#
oh and is there still a way to keep variables throughout a world? even if the actor that created it was removed? so basically i have 2 buttons that has 2 states that allows or disallows music to be playing. These buttons are only shown during the game is being played. the default state of the 2 buttons are allowing music being played. during the game u can change these to disallow. How would i keep it so that even when these two buttons are removed, when they get readded again they will be in the state that was set (so still disallowing the music).
SPower SPower

2012/7/18

#
Create instance variables for them:
1
2
private Someclass firstObject;
private Someclass secondObject;
Somewhere further, initialize them:
1
2
firstObject = new Someclass();
secondObject = new Someclass();
Add them to the world:
1
2
addObject(firstObject, x,y);
addObject(secondObject, x,y);
Remove them:
1
2
removeObject(firstObject);
removeObject(secondObject);
And later on, add them again.
gusbus123 gusbus123

2012/7/18

#
@SPower soz i forgot to mention the world will be different as well. So they get removed when it switches back to main world, then readded when it enters the secondary world again.
SPower SPower

2012/7/18

#
With secundary world, you mean another subclass of world?
gusbus123 gusbus123

2012/7/18

#
i have 3 subclasses of world. The 2 buttons are in the second and third sublclasses only. but in the creation of both worlds (as in when switching from the 1st subclass into one of the other 2) they get removed and readded with the default settings.
SPower SPower

2012/7/18

#
Why don't you look at the value the buttons must have, and create them with that info?
danpost danpost

2012/7/18

#
In your main world, create a field to hold a reference to the secondary world.
1
SecondaryWorld world2 = null;
Then, in the main world constructor, add this line.
1
world2 = new SecondaryWorld(this);
Finally, when you want to go to the secondary world, use this:
1
Greenfoot.setWorld(world2);
Your secondary world will just be 'on hold' while in the main world. You create a field in your secondary world to hold a reference to your main world in the same way, using the passed parameter, and return to your main world the same way, using that field. Also, you can still use methods in your main world through the passed parameter in the secondary world. For example, while in the secondary world:
1
world1.backgroundMusic.stop())
gusbus123 gusbus123

2012/7/18

#
Ok thx. Ill take a look at that. thx
SPower SPower

2012/7/18

#
@danpost your solution would work fine, only my solution will take up less memory: you don't keep a world object in the heap :)
There are more replies on the next page.
1
2
3