Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Programming java game - HELP PLEASE

Options
  • 10-04-2011 7:42pm
    #1
    Registered Users Posts: 108 ✭✭


    I am currently trying to programme a bank robbing game but I cant seem to get the sound working and could do with a little help. I have checked online for some pointers but havent found anything of interest yet...

    Bare in mind that I have only started trying to learn java so I dont have much of a clue atm :P

    This is the code I am using atm:

    import java.io.*;
    import java.net.URL;
    import javax.sound.sampled.*;

    public enum SoundEffect {
    SHOOT("ar.wav"), // shooting
    WALK("footsteps.wav"), // gong
    MUSIC("antichrist.mp3"); // bullet

    // Nested class for specifying volume
    public static enum Volume {
    MUTE, LOW, MEDIUM, HIGH
    }

    public static Volume volume = Volume.LOW;

    // Each sound effect has its own clip, loaded with its own sound file.
    private Clip clip;

    // Constructor to construct each element of the enum with its own sound file.
    SoundEffect(String soundFileName) {
    try {
    // Use URL (instead of File) to read from disk and JAR.
    URL url = this.getClass().getClassLoader().getResource(soundFileName);
    // Set up an audio input stream piped from the sound file.
    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url);
    // Get a clip resource.
    clip = AudioSystem.getClip();
    // Open audio clip and load samples from the audio input stream.
    clip.open(audioInputStream);
    } catch (UnsupportedAudioFileException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (LineUnavailableException e) {
    e.printStackTrace();
    }
    }

    // Play or Re-play the sound effect from the beginning, by rewinding.
    public void play() {
    if (volume != Volume.MUTE) {
    if (clip.isRunning())
    clip.stop(); // Stop the player if it is still running
    clip.setFramePosition(0); // rewind to the beginning
    clip.start(); // Start playing
    }
    }

    // Optional static method to pre-load all the sound files.
    static void init() {
    values(); // calls the constructor for all the elements
    }
    }



    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    // Testing the SoundEffect enum in a Swing application
    public class SoundEffectDemo extends JFrame {

    // Constructor
    public SoundEffectDemo() {
    // Pre-load all the sound files
    SoundEffect.init();
    SoundEffect.volume = SoundEffect.Volume.LOW; // un-mute

    // Set up UI components
    Container cp = this.getContentPane();
    cp.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
    JButton btnSound1 = new JButton("Sound 1");
    btnSound1.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    SoundEffect.EXPLODE.play();
    }
    });
    cp.add(btnSound1);
    JButton btnSound2 = new JButton("Sound 2");
    btnSound2.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    SoundEffect.GONG.play();
    }
    });
    cp.add(btnSound2);
    JButton btnSound3 = new JButton("Sound 3");
    btnSound3.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    SoundEffect.SHOOT.play();
    }
    });
    cp.add(btnSound3);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("Test SoundEffct");
    this.pack();
    this.setVisible(true);
    }

    public static void main(String[] args) {
    new SoundEffectDemo();
    }
    }


    Any useful comments would be much appreciated.


Comments

  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    I understand the drive to do it all from scratch, especially for the sake of learning, but have you considered using some kind of game class library?
    It takes care of a lot of the laborious stuff and leaves you with a more convenient code interface. It also provides a good structure for managing game objects.
    Once you can jump past the basic foundations of the engine, you can actually focus on making the game, which is still a tonne of work, but you get right into the interesting/rewarding part, which (IMO) is seeing the game start to come together.


Advertisement