Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 109,154 Java Programmers for FREE! Ask your question and get quick answers from experts. There are 1,121 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



playing sound on collision

 
Reply to this topicStart new topic

playing sound on collision

JavaArthur
post 30 Jun, 2008 - 08:31 PM
Post #1


New D.I.C Head

*
Joined: 30 Jun, 2008
Posts: 1

I would like to know how to play a sound on a rectangle collision. I am using a boolean variable on an 'if' that detects collision and if so it changes the boolean from 'false' to true. I imported the sound in the constructor of my code and I try to play it from there.
Here is my full code. The sound I am trying to play is "snd1".
CODE

package david;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.net.*;
import java.io.*;
import java.applet.AudioClip;

public class G1P3 extends JFrame{
    Image ball;
    boolean sound = false;
    URL crash = this.getClass().getResource("Door Slam.wav");
    AudioClip snd = JApplet.newAudioClip(crash);
Image ibullet;
    final int WIDTH1 = 800, HEIGHT1 = 600;
    double p1speed;
    int shot;
    final int up = 4, right = 1, down = 2, left = 3;
    int p1Direction = down;
    Rectangle bullet = new Rectangle(shot, shot, 10, 25);
    Rectangle p1 = new Rectangle( 150, 100, 31, 31);
    Rectangle o5 = new Rectangle( 60, 30, 400, 10);
//    Rectangle o1 = new Rectangle( 450, 0, 10, 200);
    Rectangle o2 = new Rectangle( 60, 500, 400, 50);
//    Rectangle o3 = new Rectangle( 60, 504, 400, 10);
//    Rectangle o4 = new Rectangle(50, 0, 0, 0);
    Rectangle b1 = new Rectangle( 60, 0, 10, 550);
    Rectangle b2 = new Rectangle( 450, 0, 10, 550);
    @SuppressWarnings("empty-statement")
        public G1P3(){
        super("Moving Test");
        setSize(WIDTH1, HEIGHT1);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        Move m = new Move();
        m.start();
        URL song = this.getClass().getResource("TingTings.wav");
        AudioClip tingtings = JApplet.newAudioClip(song);
//        tingtings.loop();
        URL crash1 = this.getClass().getResource("Door Slam.wav");
        AudioClip snd1 = JApplet.newAudioClip(crash1);
        if (sound = true){snd1.play();}

        try
            {
            URL url = this.getClass().getResource ("New Bitmap Image.bmp");
            ibullet = Toolkit.getDefaultToolkit().getImage(url);
            }catch (Exception e){};
        try
            {
            URL url = this.getClass().getResource ("ball.jpeg");
            ball = Toolkit.getDefaultToolkit().getImage(url);
            }catch (Exception e){};
//                    while (true){
//        System.out.println(p1speed + "    " + p1Direction);}
        }
    @Override
        public void paint(Graphics g){
        super.paint(g);
        g.setColor(Color.BLUE);
        g.drawImage( ball, p1.x, p1.y, this);
//        g.fill3DRect(p1.x, p1.y, p1.width, p1.height, true);
        g.setColor(Color.RED);
        g.fill3DRect(o5.x, o5.y, o5.width, o5.height, true);
//        g.fill3DRect(o1.x, o1.y, o1.width, o1.height, true);
        g.fill3DRect(o2.x, o2.y, o2.width, o2.height, true);
//        g.fill3DRect(o3.x, o3.y, o3.width, o3.height, true);
//        g.fill3DRect(o4.x, o4.y, o4.width, o4.height, true);
        g.setColor(Color.CYAN);
        g.fill3DRect(b1.x, b1.y, b1.width, b1.height, true);
        g.fill3DRect(b2.x, b2.y, b2.width, b2.height, true);
        }
private class Move extends Thread implements KeyListener
{
        private Object snd1;
        @Override
    public void run()
    {
        addKeyListener(this);
        while (true){
            try
            {
                repaint();
                sound = false;
                if (p1.intersects(o2) || p1.intersects(b1) || p1.intersects(b2) || p1.intersects(o5)) // || p1.intersects(o3) || p1.intersects(o4) || p1.intersects(o5))
                { p1speed = -4;
                sound = true;
                }
                
                if (p1speed <= 5 )
                p1speed += .2;
                
                if(p1Direction==up){p1.y-=(int)p1speed;}
                if(p1Direction==right){p1.x+=(int)p1speed;}
                if(p1Direction==down){p1.y+=(int)p1speed;}
                if(p1Direction==left){p1.x-=(int)p1speed;}
                Thread.sleep(75);
            }catch (Exception e){break;}
        }
    }
    public void keyPressed(KeyEvent event){}
    public void keyReleased(KeyEvent event){}
    public void keyTyped(KeyEvent event)
    {
        if(event.getKeyChar()=='a'){p1Direction = left;}
        if(event.getKeyChar()=='s'){p1Direction = down;}
        if(event.getKeyChar()=='d'){p1Direction = right;}
        if(event.getKeyChar()=='w'){p1Direction = up;}
        if(event.getKeyChar()=='m'){p1speed = p1speed + 4;}
        if(event.getKeyChar()=='n'){p1speed = p1speed / 2;}
        if(event.getKeyChar()=='p'){p1Direction = p1Direction * -1;}
        if(event.getKeyChar()=='g'){p1speed = 5;}
    }
    }
public static void main (String[] args){G1P3 bob = new G1P3();}
}
User is offlineProfile CardPM

Go to the top of the page


Ellie
post 1 Jul, 2008 - 04:09 AM
Post #2


D.I.C Regular

Group Icon
Joined: 17 Jan, 2007
Posts: 369



Thanked 1 times

Dream Kudos: 150
My Contributions


CODE
if (sound = true){snd1.play();}


Should be:

CODE
if (sound == true){snd1.play();}


But this isn't going to play your sound, since the constructor is called only once, so that statement will only execute once, and when you change the sound variable it will have no effect.

You should be able to put the play() call into the same place as you're setting the sound variable, you should have access to outer class variables from the inner Move class.
User is offlineProfile CardPM

Go to the top of the page

pbl
post 1 Jul, 2008 - 04:28 AM
Post #3


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 1,909



Thanked 113 times

Dream Kudos: 75
My Contributions


CODE

        if (sound = true){snd1.play();}

Will always be true ... so kind of useless better to write

snd1.play();

and it is the only place in your code (in the constructor called once) that you actually "play" an AudioClip
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 9/5/08 05:07PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month