Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Java sound, only plays once  (Read 2420 times)
0 Members and 1 Guest are viewing this topic.
Offline SubzeroX5

Senior Newbie





« Posted 2011-09-03 23:52:16 »

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

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  
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
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  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
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.

http://www.mediafire.com/?4eubm7v6u8cyakq
 
Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-09-04 00:00:30 »

First of all, it is not recommended to use classes in the Sun package.
What's wrong with using JavaSound in javax.sound.sampled?

Offline paulscode

Senior Member


Medals: 11


Staff Sergeant


« Reply #2 - Posted 2011-09-04 03:24:19 »

You might try calling stop before you call start.  I seem to recall that being the solution to similar behavior some time ago (although I've never actually used these particular classes before)

We love death.  The US loves life.  That is the difference between us.  -Osama bin Laden, mass murderer
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SubzeroX5

Senior Newbie





« Reply #3 - Posted 2011-09-04 20:33:52 »

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 { // Holds one audio file

        private AudioClip sound; // Sound player
       private URL url; // Sound path

        Sound(String filename) {
            try {
                url = this.getClass().getResource(filename); // Get the Sound URL
               sound = Applet.newAudioClip(url); // Load the Sound                
           } catch (Exception e) {
            } // Satisfy the catch
       }

        public void playLoop() {
            sound.loop(); // Play
       }

        public void stop() {
            sound.stop(); // Play
       }

        public void play() {
            sound.play(); // Play only once
       }
    }


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();
    ///////////DoubleBufferingStuff/////////
   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();
            }
        }
    }
}

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (25 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (43 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!