I have the following code for a character controlled by the user so that when he is walking the "dirt_walking" sound plays in a loop and stops when he stops walking. However, when the character gets eaten by a wolf or bear while walking, the walking sound continues in the loop. I've been struggling to have it stop upon the character's death for several days, so if anyone has a suggestion, feel free to share. Thanks.
GreenfootSound walkSound = new GreenfootSound("dirt_walking.mp3");
public void move()
{
if (isTouching(Bear.class) || isTouching(Wolf.class))
{
walkSound.stop();
return;
}
String[] keys = {"down", "up", "right", "left"};
int[][] deltas = {{0, 2}, {0, -2}, {2, 0}, {-2, 0}};
String[] images = {null, null, "Guy.png", "Guy.mirror.png"};
boolean isMoving = false;
for (int i = 0; i < keys.length; i++) {
if (Greenfoot.isKeyDown(keys[i])) {
int dx = deltas[i][0];
int dy = deltas[i][1];
if (images[i] != null) {
setImage(images[i]);
}
setLocation(getX() + dx, getY() + dy);
if (hitObjects()) {
setLocation(getX() - dx, getY() - dy); //action-reaction
}
isMoving = true;
}
}
if (isMoving)
{
if (!walkSound.isPlaying())
{
walkSound.playLoop();
}
}
else
{
walkSound.stop();
}
}

