Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Programming java game - HELP PLEASE

  • 10-04-2011 06:42PM
    #1
    Registered Users, Registered Users 2 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,144 ✭✭✭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