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

2014/2/27

Getting Sound to Stop playing and to only play once

SierraBravo SierraBravo

2014/2/27

#
So i am programming a screen in which there are three options to select. as you mouse of the options, a sound is played. the issue is that as it is constantly checking if my mouse is there, it is constantly playing the sound while the mouse is there as opposed to only playing it once.
public class TripleRsmall extends TripleR
{
    private boolean onThis=false;


    public void act() 
    {
        if(Greenfoot.mouseMoved(null)){
        onThis = Greenfoot.mouseMoved(this);
    }    
    if(onThis){
        setImage("Street-TripleR_BIG.png");
        
       Greenfoot.playSound("MotorcycleStart.mp3");
    }
    else{
        setImage("Street-TripleR_SMALL.png");
    }
  }
}
SierraBravo SierraBravo

2014/2/27

#
I am aware of why the issue is occurring but do not know how i should go about solving it. thanks.
erdelf erdelf

2014/2/27

#
Save the music as reference, and play through the reference
GreenfootSound motorcycle = new GreenfootSound("MotorcycleStart.mp3");
and then change line 14 to
if(!motorcycle.isPlaying()) motorcycle.start();
SierraBravo SierraBravo

2014/2/27

#
the program cannot find the start() method
SierraBravo SierraBravo

2014/2/27

#
there is a video of the issue (ignore the bikes jumping around. its that when i move the mouse over them it starts everytime it checks if the mouse is there.. Picture of the actual code and start() issue
davmac davmac

2014/2/27

#
Check the documentation for GreenfootSound - there is no start() method. You probably want the play() method.
SierraBravo SierraBravo

2014/2/27

#
That was it. Thanks davmac
You need to login to post a reply.