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

2013/5/26

How to place sound once

jaydjay jaydjay

2013/5/26

#
How do i make an introduction sound play only once and have actors not move until the sound is finished? I have a playintro command and also a isintrostillplaying command. I have not put it into the act method to stop it from an infinite loop. Do i make intro sound in another public void?
jaydjay jaydjay

2013/5/26

#
anyone?
If you haven't used it already, this site is very useful, it's the API for greenfoot: http://www.greenfoot.org/files/javadoc/ It has all the methods and objects used in greenfoot. GreenfootSound is where you'll find the methods to play sound. There is a method called isPlaying(), which will return true if the GreenfootSound is playing, and false otherwise. There is also a play() method (which I think you already knew) which will play the sound. I may be wrong, but it seems to me that your problem is that you keep indefinitely call play() in an act method somewhere (I'm guessing in your world). What you need to do is tell it to play once. You can do that using something like this:
//In whatever class you have the sound play
private boolean soundHasPlayed = false;
private GreenfootSound mySound;

//Constructor and other methods
...

public void act()
{
    if (!soundHasPlayed)
    {
        mySound.play();
        soundHasPlayed = true;  //This will cause this if block never to run again, unless you have code somewhere that turns it to false again.
    }
    ...
}
With your actors not moving, you need to have a public method in whatever class you have the sound playing (if it's not in your World, I strongly recommend putting it there) that will return a boolean value, true if the sound is playing, or false otherwise (hmm, that isPlaying() method might come in handy). I hoped that helped!
danpost danpost

2013/5/27

#
jaydjay wrote...
How do i make an introduction sound play only once and have actors not move until the sound is finished? I have a playintro command and also a isintrostillplaying command. I have not put it into the act method to stop it from an infinite loop. Do i make intro sound in another public void?
This will do the trick:
private boolean soundhasplayed;
// in act
if(!soundhasplayed)
{
    playintro();
    while (isintrostillplaying) delay(1); // this pauses everything
    soundhasplayed = true;
}
jaydjay jaydjay

2013/5/28

#
Thank you for the help! Your information was really helpful!
jaydjay jaydjay

2013/5/29

#
I need it put into the void act how would i do it? The sound keeps repeating itself.
boolean hasintroPlayed = false;
        {
            if (!hasintroPlayed)  
             {  
                 {
                     playIntro();  
                     while (IntroStillPlaying())  
                     hasintroPlayed = true;  
                    }
                }  
        }
danpost danpost

2013/5/29

#
Move the first line outside of the method it is in.
jaydjay jaydjay

2013/5/29

#
danpost wrote...
Move the first line outside of the method it is in.
I dont quite understand sorry, i've tried doing this already. But it doesn't work
public void act()
boolean hasintroPlayed = false;  
 
        {  
            if (!hasintroPlayed)    
             {    
                 {  
                     playIntro();    
                     while (IntroStillPlaying())    
                     hasintroPlayed = true;    
                    }  
                }    
        }  
danpost danpost

2013/5/29

#
Switch the first two lines around.
What's he's saying is to put the hasIntroPlayed outside of the method. The sound keeps looping because you made a variable inside a method, that turns back to false every time the method is called (which is every frame). If you have the variable outside the method, it will be available to all your code inside the class, and won't ever change unless you tell it to.
You need to login to post a reply.