I've got the answer to my problem.
Since, Im going to make a downloadable and an Applet version of my game, I used AudioClip instead. So, If I want to make this downloadable version an Applet, I can just change the MainConfig constructor into an init, and remove the class, UltraPong.
Also, the sound will play, everytime the ball intersects the paddle.
Sound
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
| import java.applet.Applet; import java.applet.AudioClip; import java.net.*;
public class Sound { private AudioClip sound; private URL url; Sound(String filename) { try { url = this.getClass().getResource(filename); sound = Applet.newAudioClip(url); } catch (Exception e) { } }
public void playLoop() { sound.loop(); }
public void stop() { sound.stop(); }
public void play() { sound.play(); } } |
MainConfig
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| import java.applet.Applet; import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import javax.swing.*; import ultrapong.levels.*; import ultrapong.levels.Config.*; import ultrapong.startscreen.Menu;
public class MainConfig extends Applet implements Runnable {
public int setWidth, setHeight; public Player player; public Thread mainLoop; public Level currentLevel; public int counter; public AI computer = new AI(); public Ball ball = new Ball(); public BoxLevel bx = new BoxLevel(); public Menu m = new Menu(); public Graphics dbg; public Image dbImage;
public MainConfig() { player = new Player(); addKeyListener(new KL()); addMouseMotionListener(new MML()); addMouseListener(new ML()); setFocusable(true); setWidth = 506; setHeight = 498; currentLevel = m; }
public void addNotify() { super.addNotify(); mainLoop = new Thread(this); mainLoop.start(); }
@Override public void update(Graphics g) { dbImage = createImage(getWidth(), getHeight()); dbg = dbImage.getGraphics(); paint(dbg); g.drawImage(dbImage, 0, 0, this); }
public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D) g; if (currentLevel == m) { m.drawLevelElements(g2d); if (m.startPressed) { currentLevel = bx; } } else if (currentLevel == bx) { bx.drawBackground(g2d); bx.drawLevel(g2d); bx.drawLevelObjects(g2d); } }
public class MML extends MouseMotionAdapter {
@Override public void mouseMoved(MouseEvent e) { m.mouseMoved(e); } }
public class ML extends MouseAdapter {
@Override public void mousePressed(MouseEvent e) { m.mousePressed(e); }
@Override public void mouseReleased(MouseEvent e) { m.mouseReleased(e); } }
public class KL extends KeyAdapter {
@Override public void keyPressed(KeyEvent e) { if (currentLevel == bx) { bx.player.keyPressed(e); } }
@Override public void keyReleased(KeyEvent e) { if (currentLevel == bx) { bx.player.keyReleased(e); } } }
public void updateLevels() { if (currentLevel == bx) { bx.update(ball, player, computer, currentLevel); } }
public void run() {
float startingTime = System.currentTimeMillis(); float cumTime = System.currentTimeMillis() - startingTime; while (true) { float timePassed = System.currentTimeMillis() - cumTime; cumTime += timePassed; updateLevels(); repaint(); try { Thread.sleep(20); } catch (Exception e) { System.err.toString(); } } } } |