When the ball intersects the paddle, it plays the sound.But when the ball intersects the paddle again, the sound won’t play.
Here are some of my source codes.
Sound
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.*;
import sun.audio.AudioData;
import sun.audio.AudioDataStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
import sun.audio.ContinuousAudioDataStream;
public class Sound {
public AudioPlayer MGP = AudioPlayer.player;
public AudioStream BGM;
public AudioData MD;
public ContinuousAudioDataStream loop = null;
public AudioDataStream play = null;
public File file;
public URL url;
public Sound(String dir) {
url = this.getClass().getResource(dir);
try {
file = new File(url.toURI());
} catch (URISyntaxException e) {
file = new File(url.getPath());
}
try {
System.out.println("the url is " + file.getAbsolutePath());
BGM = new AudioStream(new FileInputStream(file));
MD = BGM.getData();
loop = new ContinuousAudioDataStream(MD);
play = new AudioDataStream(MD);
} catch (IOException e) {
}
}
public void play() {
MGP.start(play);
}
public void playLoop() {
play = loop;
MGP.start(play);
}
public void stop() {
MGP.stop(play);
}
}
Ball
import java.awt.Rectangle;
import java.util.Random;
import java.awt.Image;
import javax.swing.ImageIcon;
public class Ball {
public int x;
public int y;
public int dx = 0;
public int dy = 0;
public int diameter;
public Image ballImg;
public Player player = new Player();
boolean bouncedX = false;
boolean bouncedY = false;
//////////Sounds//////////////
public Sound hit = new Sound("/ultrapong/sounds/paddle/hit.wav");
public Ball() {
x = 250;
y = 250;
dx = 3;
dy = 3;
diameter = 5;
ballImg = new ImageIcon(this.getClass().getResource("/ultrapong/Ball.png")).getImage();
ranBouncerX = new Random();
ranBouncerY = new Random();
}
public Random random = new Random();
public void update() {
if (y < 0 || y + diameter > 460) {
for (int i = 250; i < 300 && i > 200; i++) {
y = random.nextInt(i);
}
for (int i = 250; i < 300 && i > 200; i++) {
x = random.nextInt(i);
}
dx = 2;
dy = 2;
} else if (x + diameter + (diameter / 2) > 500 || x < 0) {
dx = bounceX(dx);
}
x = x + dx;
y = y + dy;
}
public void updateBounceX(Rectangle p, Rectangle b) {
if (p.intersects(b)) {
dy = bounceY(dy);
hit.play();
}
}
public void updateBounceTileX(Rectangle p, Rectangle b) {
int leftBorder = p.x;
int rightBorder = p.x + p.width;
int middleBorderX = p.x + (p.width / 2);
if (p.intersects(b) && b.x <= rightBorder && b.x + b.width > middleBorderX) {
if (!bouncedX) {
dx = bounceX(dx);
bouncedX = true;
}
if (bouncedX) {
dx = dx * 1;
}
} else if (p.intersects(b) && b.x + b.width >= leftBorder && b.x + b.width < middleBorderX) {
if (!bouncedX) {
dx = bounceX(dx);
bouncedX = true;
}
if (bouncedX) {
dx = dx * 1;
}
} else if (!p.intersects(b)) {
bouncedX = false;
}
}
public void updateBounceTileY(Rectangle p, Rectangle b) {
int topBorder = p.y;
int bottomBorder = p.y + p.height;
int middleBorderY = p.y + (p.height / 2);
if (p.intersects(b) && b.y <= bottomBorder && b.y + b.height > middleBorderY) {
if (!bouncedY) {
dy = bounceY(dy);
bouncedX = true;
}
if (bouncedY) {
dy = dy * 1;
}
} else if (p.intersects(b) && b.y + b.height >= topBorder && b.y + b.height < middleBorderY) {
if (!bouncedY) {
dy = bounceY(dy);
bouncedY = true;
}
if (bouncedY) {
dy = dy * 1;
}
} else if (!p.intersects(b)) {
bouncedY = false;
}
}
public Rectangle ball() {
return new Rectangle(getX(), getY(), diameter(), diameter());
}
public Image getImage() {
return ballImg;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int diameter() {
return diameter;
}
public void velocityX(int vx) {
this.dx = vx;
}
public void velocityY(int vy) {
this.dy = vy;
}
public Random ranBouncerX;
public int bounceX(int d) {
int dd = (d < 0) ? -1 : 1;
int n = ranBouncerX.nextInt();
if (n <= 0) {
n = 1 - n;
}
return ((n % 6) + 2) * -dd;
}
public Random ranBouncerY;
public int bounceY(int d) {
int dd = (d < 0) ? -1 : 1;
int n = ranBouncerY.nextInt();
if (n <= 0) {
n = 1 - n;
}
return ((n % 6) + 2) * -dd;
}
public int getDx() {
return dx;
}
public int getDy() {
return dy;
}
}
Here are all of my source codes.