I believe the Greenfoot method 'playSound' causes the sound to be looped automatically; and there is no control over that sound once it is started. Making a GreenfootSound object will allow the methods in its class to control the sound object. Just by using the method 'play', it will play just one time; there is no need to stop or pause it.
This is still making it loop: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Boat here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Boat extends Actor
{
private static GreenfootSound soundSound = new GreenfootSound("crack.mp3");
/**
* Act - do whatever the Boat wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (getX() >1185)
{ move(-5);
}
if (getX() == 1185)
{
getWorld().addObject(new Label(), 400, 249);
soundSound.play();
}
}
}
Ok. I think what is happening is that your actor is still at x=1185; and as soon as the sound stops, the code re-starts it. You may need to add a boolean field 'soundPlayed' to the code and set it to 'true' when starting the sound:
// add instance field
private boolean soundPlayed;
// the 'if' block
if (getX() == 1185 && !soundPlayed)
{
getWorld().addObject(new Label(), 400, 249);
soundSound.play();
soundPlayed = true;
}
You probably had a bunch of Label objects on top of each other also.