Fix chest and key sound problems

Signed-off-by: Chris Cromer <chris@cromer.cl>
This commit is contained in:
Chris Cromer 2019-09-30 13:46:44 -03:00
parent b1a307a8e5
commit db72bcf5dc
5 changed files with 27 additions and 24 deletions

Binary file not shown.

View File

@ -159,7 +159,7 @@ public class Lienzo extends Canvas implements Constantes {
try { try {
backgroundMusic = escenario.getSounds().get(Sound.SoundType.BACKGROUND); backgroundMusic = escenario.getSounds().get(Sound.SoundType.BACKGROUND);
backgroundMusic.setLoops(Clip.LOOP_CONTINUOUSLY); backgroundMusic.setLoops(Clip.LOOP_CONTINUOUSLY);
backgroundMusic.run(); backgroundMusic.play();
} }
catch (SoundException e) { catch (SoundException e) {
logger.warning(e.getMessage()); logger.warning(e.getMessage());
@ -259,7 +259,12 @@ public class Lienzo extends Canvas implements Constantes {
if (!gameOverRan) { if (!gameOverRan) {
stopBackgroundMusic(); stopBackgroundMusic();
new Thread(escenario.getSounds().get(Sound.SoundType.GAME_OVER)).start(); try {
escenario.getSounds().get(Sound.SoundType.GAME_OVER).play();
}
catch (SoundException e) {
logger.warning(e.getMessage());
}
stopThreads(); stopThreads();
@ -323,7 +328,12 @@ public class Lienzo extends Canvas implements Constantes {
public void win() { public void win() {
stopBackgroundMusic(); stopBackgroundMusic();
new Thread(escenario.getSounds().get(Sound.SoundType.SUCCESS)).start(); try {
escenario.getSounds().get(Sound.SoundType.SUCCESS).play();
}
catch (SoundException e) {
logger.warning(e.getMessage());
}
stopThreads(); stopThreads();
JOptionPane.showMessageDialog(null, "Ganaste!"); JOptionPane.showMessageDialog(null, "Ganaste!");

View File

@ -19,6 +19,7 @@ import cl.cromer.azaraka.Celda;
import cl.cromer.azaraka.Constantes; import cl.cromer.azaraka.Constantes;
import cl.cromer.azaraka.Escenario; import cl.cromer.azaraka.Escenario;
import cl.cromer.azaraka.sound.Sound; import cl.cromer.azaraka.sound.Sound;
import cl.cromer.azaraka.sound.SoundException;
import cl.cromer.azaraka.sprite.Animation; import cl.cromer.azaraka.sprite.Animation;
import cl.cromer.azaraka.sprite.AnimationException; import cl.cromer.azaraka.sprite.AnimationException;
import cl.cromer.azaraka.sprite.SheetException; import cl.cromer.azaraka.sprite.SheetException;
@ -183,7 +184,12 @@ public class Enemy extends Object implements Constantes {
if (getEscenario().getCanvas().getPlayer().getHealth() > 0) { if (getEscenario().getCanvas().getPlayer().getHealth() > 0) {
logger.info("Attacked player at x: " + x + " y: " + y); logger.info("Attacked player at x: " + x + " y: " + y);
new Thread(getEscenario().getSounds().get(Sound.SoundType.ENEMY_ATTACK)).start(); try {
getEscenario().getSounds().get(Sound.SoundType.ENEMY_ATTACK).play();
}
catch (SoundException e) {
e.printStackTrace();
}
getEscenario().getCanvas().getPlayer().loseHealth(2); getEscenario().getCanvas().getPlayer().loseHealth(2);
try { try {

View File

@ -389,11 +389,11 @@ public class Player extends Object implements Constantes {
if (keySound.isPlaying()) { if (keySound.isPlaying()) {
keySound.stop(); keySound.stop();
} }
keySound.play();
} }
catch (SoundException e) { catch (SoundException e) {
logger.warning(e.getMessage()); logger.warning(e.getMessage());
} }
keySound.run();
} }
/** /**
@ -413,11 +413,11 @@ public class Player extends Object implements Constantes {
if (chestSound.isPlaying()) { if (chestSound.isPlaying()) {
chestSound.stop(); chestSound.stop();
} }
chestSound.play();
} }
catch (SoundException e) { catch (SoundException e) {
logger.warning(e.getMessage()); logger.warning(e.getMessage());
} }
chestSound.run();
gainHealth(1); gainHealth(1);

View File

@ -26,7 +26,7 @@ import java.util.logging.Logger;
/** /**
* This class handles sound * This class handles sound
*/ */
public class Sound implements Runnable, Constantes { public class Sound implements Constantes {
/** /**
* The sound clip to play * The sound clip to play
*/ */
@ -61,12 +61,11 @@ public class Sound implements Runnable, Constantes {
logger.warning(e.getMessage()); logger.warning(e.getMessage());
} }
try { try {
/*DataLine.Info info = null; DataLine.Info info = null;
if (audioInputStream != null) { if (audioInputStream != null) {
info = new DataLine.Info(Clip.class, audioInputStream.getFormat()); info = new DataLine.Info(Clip.class, audioInputStream.getFormat());
} }
sound = (Clip) AudioSystem.getLine(info);*/ sound = (Clip) AudioSystem.getLine(info);
sound = AudioSystem.getClip();
} }
catch (LineUnavailableException e) { catch (LineUnavailableException e) {
logger.warning(e.getMessage()); logger.warning(e.getMessage());
@ -74,6 +73,7 @@ public class Sound implements Runnable, Constantes {
try { try {
if (audioInputStream != null) { if (audioInputStream != null) {
sound.open(audioInputStream); sound.open(audioInputStream);
sound.stop();
} }
} }
catch (LineUnavailableException | IOException e) { catch (LineUnavailableException | IOException e) {
@ -87,7 +87,7 @@ public class Sound implements Runnable, Constantes {
* Play the sound * Play the sound
* @throws SoundException Thrown if the sound clip is null * @throws SoundException Thrown if the sound clip is null
*/ */
private void play() throws SoundException { public void play() throws SoundException {
if (sound == null) { if (sound == null) {
throw new SoundException("Sound is null!"); throw new SoundException("Sound is null!");
} }
@ -143,19 +143,6 @@ public class Sound implements Runnable, Constantes {
sound.loop(loops); sound.loop(loops);
} }
/**
* Run the sound in a thread
*/
@Override
public void run() {
try {
play();
}
catch (SoundException e) {
logger.warning(e.getMessage());
}
}
/** /**
* Set the volume of the sound * Set the volume of the sound
* *