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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | 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(); } } |