Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1] 2 3 4
1  Game Development / Game Mechanics / Re: Mathematical curves. on: 2014-04-13 00:13:58
If you want to play around with lots of different easing functions it makes things easier if you define yourself a function class that you can then scale, shift and combine with other functions.
Here is an example.
Func.java
http://pastebin.com/mFexrzMH
Graph.java
http://pastebin.com/2bPFcahz

btw. if you use Java 8 you can make your function definitions a lot shorter then in that example.

Of course this will make the calculation slower, but that shouldn't matter for easing animations.
2  Game Development / Newbie & Debugging Questions / Re: My most inefficient way of implementing running texts. How to optimize? on: 2014-04-06 01:12:50
Here is another example.
It uses double buffering.

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  
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.Font;
import java.util.ArrayList;

public class Text extends JPanel {
    static final int CHAR_WIDTH = 7;
    static final int CHAR_HEIGHT = 14;
   
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        Text text = new Text();
        frame.add(text);
        text.setPreferredSize(new Dimension(800, 600));
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        text.runningText("This is a running text. Words that don't fit completely on a line will automatically be moved to the next line.", 30, 30, 15, 100);
    }
   
    private void runningText(String str, int x, int y, int maxWidth, int delay) {
        new Thread() {
            @Override
            public void run() {
                String[] lines = toLines(str, maxWidth);
                int maxLineLength = maxLength(lines);
               
                Graphics2D g = image.createGraphics();
               
                g.setFont(new Font("monospaced", Font.PLAIN, 12));
                g.setColor(Color.BLACK);
                g.drawRect(x-5, y-5, maxWidth*CHAR_WIDTH + 10, lines.length*CHAR_HEIGHT + 10);
               
                for(int j = 1; j < str.length(); j++) {
                    g.setColor(Color.WHITE);
                    g.fillRect(x, y, maxLineLength*CHAR_WIDTH, lines.length*CHAR_HEIGHT);
                    g.setColor(Color.BLACK);
                    int count = 0;
                    loop:
                    for(int i = 0; i < lines.length; i++) {
                        for(int k = 0; k < lines[i].length(); k++) {
                            g.drawString(lines[i].substring(k, k+1), x+k*CHAR_WIDTH, y+(i+1)*CHAR_HEIGHT);
                            count++;
                            if(count == j) break loop;
                        }
                    }
                    repaint();
                    try {
                        Thread.sleep(delay);
                    } catch(Exception e) {
                    }
                }
               
                g.dispose();
            }
        }.start();
    }
   
    BufferedImage image = null;
   
    public Text() {
        initImage();
    }
   
    public void initImage() {
        int w = getWidth();
        int h = getHeight();
        if(w <= 0 || h <= 0) {
            w = 800;
            h = 600;
        }
        image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, w, h);
        g.dispose();
    }
   
    @Override
    public void setPreferredSize(Dimension d) {
        super.setPreferredSize(d);
        initImage();
    }
   
    static String[] toLines(String str, int maxLength) {
        ArrayList<String> lines = new ArrayList<>();
        String[] words = str.split("\\s");
        String line = "";
        int length = 0;
        for(String word: words) {
            if(length+word.length()+1 > maxLength) {
                lines.add(line);
                line = "";
                length = 0;
            }
            if(length > 0) {
                line += " ";
                length += 1;
            }
            line += word;
            length += word.length();
        }
        if(line.length() > 0) lines.add(line);
        return lines.toArray(new String[lines.size()]);
    }
   
    static int maxLength(String[] lines) {
        int length = 0;
        for(String line: lines) length = Math.max(length, line.length());
        return length;
    }

    @Override
    public void paint(Graphics _g) {
        Graphics2D g = (Graphics2D)_g;
        g.scale(3,3);
        g.drawImage(image, 0, 0, null);
    }
}
3  Discussions / Miscellaneous Topics / Re: new computer purchase advice on: 2014-04-05 15:45:53
For the power supply the most important thing is not the wattage but the overall quality of the psu.
http://www.pcworld.com/article/2025425/how-to-pick-the-best-pc-power-supply.html
The one built into "system 2" is most probably low quality.
Don't be afraid to pay around 100$ for the psu.
4  Game Development / Game Mechanics / Re: Sidescroller jumping/collision (Not Java, but Javascript) on: 2014-04-03 00:45:52
There are some things you can do to make this kind of stuff easier to handle.
For once you should start to think in vectors.
http://www.mathsisfun.com/algebra/vectors.html

You can use vectors to represent a position, velocity, acceleration, etc.
It usually simplifies your code if you make mathematical entities - e.g. vectors - immutable.
That means you don't ever change such an object, instead you always create new ones.

Here is an example for a 2d vector.
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  
function Vec2D(x,y) {
    this.x = x;
    this.y = y;
};

Vec2D.prototype.dotProduct = function(v) {
    return this.x*v.x+this.y*v.y;
};
Vec2D.prototype.crossProduct = function(v) {
    return this.x*v.y-this.y*v.x;
};
Vec2D.prototype.getLength = function() {
    return Math.sqrt(this.x*this.x+this.y*this.y);
};
Vec2D.prototype.add = function(v) {
    return new Vec2D(this.x+v.x, this.y+v.y);
};
Vec2D.prototype.addX = function(dx) {
    return new Vec2D(this.x+dx, this.y);
};
Vec2D.prototype.addY = function(dy) {
    return new Vec2D(this.x, this.y+dy);
};
Vec2D.prototype.withX = function(x) {
    return new Vec2D(x, this.y);
};
Vec2D.prototype.withY = function(y) {
    return new Vec2D(this.x, y);
};
Vec2D.prototype.sub = function(v) {
    return new Vec2D(this.x-v.x, this.y-v.y);
};
Vec2D.prototype.mul = function(m) {
    return new Vec2D(this.x*m, this.y*m);
};
Vec2D.prototype.div = function(d) {
    return new Vec2D(this.x/d, this.y/d);
};
Vec2D.prototype.neg = function() {
    return new Vec2D(-this.x, -this.y);
};
Vec2D.prototype.toString = function() {
    return "("+this.x+", "+this.y+")";
};
Vec2D.prototype.equals = function(v) {
    return this.x === v.x && this.y === v.y;
};


Another useful strategy is to seperate your code into several small pieces and put every piece into it's own function.
Unfortunately due to JavaScripts bad design, that can be a little tricky.

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  
Player.prototype.update = (function() {
  var oldPos, _this;
 
  var move = function(v) {
    oldPos = _this.pos;
    _this.pos = _this.pos.add(v);
  };
 
  var moveBack = function() {
    _this.pos = oldPos;
  };
 
  var checkCollision = function(entities) {
    for(var i = 0; i < entities.length; i++) {
      if(!entities[i].passable && _this.intersects(entities[i])) return true;
    }
    return false;
  };
 
  var tryToMove = function(d, entities) {
    move(d);
    if(checkCollision(entities)) {
      moveBack();
      return true;
    }
    return false;
  };
 
  var update = function(entities) {
    _this = this;
    if(Keyboard.isPressed(Keyboard.D)) {
      this.velocity = this.velocity.withX(10);
    } else if(Keyboard.isPressed(Keyboard.A)) {
      this.velocity = this.velocity.withX(-10);
    } else {
      this.velocity = this.velocity.withX(0);
    }
    if(!this.isJumping && Keyboard.isPressed(Keyboard.SPACE)) {
      this.isJumping = true;
      this.velocity = this.velocity.addY(-this.jumpSpeed);
    }
    if(this.velocity.y < this.grav) {
      this.velocity = this.velocity.addY(this.grav);
    }
    var steps = Math.max(this.velocity.x, this.velocity.y);
    var distance = this.velocity.div(steps);
    var isColliding = false;
    for(var i = 0; i < move && !isColliding; i++) {
      isColliding = tryToMove(distance, entities);
    }
  }
  return update;
})();
5  Game Development / Game Mechanics / Re: Corners of rotated rectangle wrong on: 2014-03-02 00:40:27
There is no error in the code you posted.
If getAngleInRadians returns 0, then translatePoint will always return the same point that was put in.
So there must be somthing wrong elsewhere in your code.
6  Game Development / Newbie & Debugging Questions / Re: How to intersect a oval and a rect ? on: 2014-02-15 19:22:00
Here is a simply example. The ball bounces of the corners of the rectangle at the correct angle.
http://pastebin.com/ez8VWAj2
7  Discussions / Miscellaneous Topics / Re: What is your typing speed? on: 2014-02-15 03:14:17
Actually pretty damn addicting Tongue

If you find that one addicting you should check out z-type.
http://phoboslab.org/ztype/
8  Game Development / Newbie & Debugging Questions / Re: Server/Client on: 2014-02-04 09:15:25
I don't see a call to repaint in your code.
There are a few things you could try.
Use DataInput/OutputStream and don't buffer the streams.
Call Socket.setTcpNoDelay(true); and Socket.setTrafficClass(0x10); before you connect the socket. i.e. between line 16 and 17 in your code.
9  Game Development / Game Mechanics / Re: Raytracing - Tutorials? on: 2014-02-01 19:22:53
There is an example of a very basic raytracer here
http://www.typescriptlang.org/Samples/#Raytracer
You could try and translate the code to Java.
10  Game Development / Game Play & Game Design / Re: Creating a circular "grid" ? on: 2013-11-11 19:34:47
You need to multiply by 2*pi to get the angle in rads.
11  Game Development / Performance Tuning / Re: Impossible race condition between threads? on: 2013-11-03 00:07:06
The problem is like nsigma wrote the complicated dependencies between the tasks.

Can't you reduce those dependencies?

Here is an example of what the code could look like.

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  
final ExecutorService threadPool = new ForkJoinPool();

ArrayList<Model> models = new ArrayList<>();
final ArrayList<Perspective> perspectives = new ArrayList<>();

ArrayList<Future<Model>> futures = new ArrayList<>();

for(Model _model: models) {
    final Model model = _model;
    threadPool.submit(new Callable<Model>() {
        public Model call() {
           
            model.update();
            boolean visible = false;
            for(Perspective p: perspectives) {
                if(model.isVisible(p)) {
                    visible = true;
                    break;
                }
            }
            if(visible) {
                model.computeBones();
                model.generateMatrices();
                return model;
            }
            return null;
           
        }
    });
}

ArrayList<Model> visibleModels = new ArrayList<>();
for(Future<Model> f: futures) {
    Model m = f.get();
    if(m != null) visibleModels.add(m);
}

To make sure values are written to memory and not just kept in registers, all methods of Model should be declared as synchronized.
12  Game Development / Performance Tuning / Re: Impossible race condition between threads? on: 2013-11-01 21:21:45
I may be misunderstanding something here, but why don't you use futures? Create a thread pool with e.g Executors.newFixedThreadPool() and then add some anonymous objects of type Callable to it. That gives you a list of Future objects and then you simply call get() on all of them to block until all threads are finished.
That way you don't need to synchronize anything and you don't need volatile either. As long as the data you pass into your Callables is immutable and your callables are side effect free, you are guaranteed to never run into any kinds of threading problems.
13  Game Development / Newbie & Debugging Questions / Re: Rotating a point around a center point algorithm on: 2013-10-26 12:44:24
Here is an example of a rotation matrix. It can be used to rotate a vector around any arbitary axis.
http://pastebin.com/Gq4mHL82
14  Discussions / Miscellaneous Topics / Re: What would you like to see in Java? on: 2013-10-08 23:45:39
Would be nice to have string interpolation in Java.
15  Game Development / Newbie & Debugging Questions / Re: Can't figure out how to do Java Audio properly. on: 2013-10-08 22:16:46
It has probably something to do with threads. Using threads incorrectly can cause all kinds of weird random errors.
16  Discussions / Miscellaneous Topics / Re: What would you like to see in Java? on: 2013-10-07 00:59:32
... I seriously hate when people try to convince others to do things the wrong way.
I couldn't agree more.
If people simply wised up and started doing everything the "Right Way"™ many conflicts could by avoided.
http://www.asimovs.com/2012_08/ref.shtml
17  Discussions / Miscellaneous Topics / Re: What would you like to see in Java? on: 2013-10-06 21:02:30
@lukasz1985
Optimization for functional programming is comming in Java 8.
https://www.youtube.com/watch?v=C_QbkGU_lqY
Scala may not be a good choice for a game engine but no JVM language really is.
Having to maintain shared state yourself is like having to manage memory yourself. For most kinds of software it's better to leave that to the compiler/runtime system.
I think immutable data is the perfect solution for multithreading. Compilers just have to get better at optimizing that stuff.
The lack of break/continue is not a big deal. The philosophy of Scala is that any loops that need break/continue should be written with recursion (Scala has nested methods and tail call optimization).
18  Game Development / Newbie & Debugging Questions / Re: Can't figure out how to do Java Audio properly. on: 2013-10-06 07:15:01
If you loop a clip this code will run continuously until you set active to false.
1  
2  
3  
while(active) {
    if(!clip.isRunning() && !smoothLoop) active = false;
}

Start a clip with smoothLoop set to true and then look at the Java program in your task manager. One of your CPU cores will have 100% load on it while the clip is playing.
19  Game Development / Newbie & Debugging Questions / Re: Can't figure out how to do Java Audio properly. on: 2013-10-06 02:13:59
But now you have an infinite loop that puts 100% load on one CPU core.
That's what the LineListener is for. So you can react to the Clip stopping without needing a loop.
And as I already mentioned you can also use a MouseListener/KeyListener to react to buttons being pressed/released.

Also if you need an isRunning() method in the AudioData class you can define it like this
1  
2  
3  
4  
5  
6  
public synchronized boolean isRunning() {
    for(Clip clip: clipsPlaying) {
        if(clip.isRunning()) return true;
    }
    return false;
}


But I don't think you even need that. AudioData has a stop method. Just call it when the button is released.
20  Discussions / Miscellaneous Topics / Re: What would you like to see in Java? on: 2013-10-06 00:35:13
...
Why not replace it with something like this:
1  
2  
3  
4  
5  
for (int i -> 0 : 20) {
    for (int i2 -> 0 : 20) {
        doOrGetSomething(i, i2);
    }
}


You could define yourself a Range class and a range method and use it like this
1  
2  
3  
for (int i: range(0,20)) {
    doOrGetSomething(i);
}

and with Java 8 you could even do this
1  
2  
3  
nested(range(0,20), range(0,20)).forEach((i,j) ->
    doOrGetSomething(i,j);
);

or
1  
2  
3  
for(Object elem: nested(array2d)) {
    doOrGetSomething(elem);
);


And one more! For loops for hashmaps like in PHP:

1  
2  
3  
4  
for( key, value : hashMap) {
  key.this();
  value.that();
}


In Java 8 you can write
1  
2  
3  
hashMap.forEach((key, value) ->
    doSomething(key, value);
);


I'd like to see more stuff done in Java2D.
You know, built in gradients, anti-aliasing, etc.
That stuff is already in Java2D. But you could also use JavaFX.
It's now part of the standard Java distribution.

Scala is way too complicated - it actualy is insanely complicated and there are things that adds to this complications.
Scala is a wonderful language. But you can't expect to understand it on one weekend.
Also some of the features of the language are meant mostly for API developers.
You don't need to know or understand all of them to use Scala.
In fact there is a Scala "Learning Environment" designed to teach programming to children.
http://www.kogics.net/kojo
Try out the "Kojo Overview Story".

The default import for collections (immutable) is a horrible assumption.
Functional programming is good but not with the approach to immutability, people are really blind to this concept, which is not good for performance.
Making immutability the default was the right decision. It helps to reduce bugs and simplifies parallel programming.
The impact on performance is small in most cases. In the few cases where it actually causes a performance issue, it's very easy to switch back to mutable collections.
21  Game Development / Newbie & Debugging Questions / Re: Can't figure out how to do Java Audio properly. on: 2013-10-05 21:46:42
If there is only a 20ms delay I guess it's best to use loop(). And then call stop(), once the fire button has been released. You can do that by either making the flamethrower AudioData object globally accessible or by adding a MouseListener when the fire button is pressed. You can add as many MouseListeners to your game as you want. That listener will then stop the sound and remove itself.
22  Game Development / Newbie & Debugging Questions / Re: Can't figure out how to do Java Audio properly. on: 2013-10-05 21:28:38
Wait, so you want to play the flamethrower sound only if it's not playing anymore. Can't you use loop()? Then it will automatically start playing again once it stopped.
Also, I edited my previous post. Take a look at the new code.
23  Game Development / Newbie & Debugging Questions / Re: Can't figure out how to do Java Audio properly. on: 2013-10-05 20:45:16
The clip class has a loop and a stop method. But after you call stop the clip will get closed automatically so don't call start or loop on it after you stopped it.
For a machine gun effect you need a delay. You probably have to create another Thread for that. You can communicate with that Thread through a volatile variable.

EDIT:
I updated my AudioData class

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  
public class AudioData {
    private byte[] data;
    private AudioFormat format;
    private volatile boolean machineGunActive = false;
    private HashSet<Clip> clipsPlaying = new HashSet<>();
   
    private synchronized void removeClip(Clip clip) {
        clipsPlaying.remove(clip);
    }
    private synchronized void addClip(Clip clip) {
        clipsPlaying.add(clip);
    }
    private synchronized void stopAllClips() {
        for(Clip clip: clipsPlaying) clip.stop();
        clipsPlaying.clear();
    }
   
    AudioData(String fileName) {
       try {
           AudioInputStream sounds = AudioSystem.getAudioInputStream(AudioData.class.getResource(fileName));
           format = sounds.getFormat();
           int size = sounds.available();
           data = new byte[size];
           sounds.read(data);
           sounds.close();
       } catch(Exception e) {
           e.printStackTrace();
       }
    }
    public Clip createClip() {
        try {
            final Clip clip = AudioSystem.getClip();
            clip.addLineListener(new LineListener() {
             public void update(LineEvent e) {
                 LineEvent.Type type = e.getType();
                 if(type == type.STOP) {
                     clip.close();
                     removeClip(clip);
                 }
             }
            });
            clip.open(format, data, 0, data.length);
            return clip;
        } catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public void play() {
        Clip clip = createClip();
        addClip(clip);
        clip.start();
    }
    public void loop(int n) {
        Clip clip = createClip();
        addClip(clip);
        clip.loop(n);
    }
    public void loopForever() {
        Clip clip = createClip();
        addClip(clip);
        clip.loop(Clip.LOOP_CONTINUOUSLY);
    }
    public void stop() {
        machineGunActive = false;
        stopAllClips();
    }
    public void stopMachineGun() {
        machineGunActive = false;
    }
    public void playMachineGun(final int delay) {
        machineGunActive = true;
        new Thread() {
            public void run() {
                while(machineGunActive) {
                    play();
                    try {
                        Thread.sleep(delay);
                    } catch(Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }
}

Try this
1  
2  
3  
4  
AudioData data = new AudioData("shoot.wav");
data.playMachineGun(100);
Thread.sleep(2000);
data.stopMachineGun();

The difference between stop() and stopMachineGun() is that stop() will stop playing immediately, while stopMachineGun() allows the sound to fade out naturally.
24  Game Development / Newbie & Debugging Questions / Re: Can't figure out how to do Java Audio properly. on: 2013-10-05 05:32:04
You could use your Sounds enum but put AudioData objects in there instead of clips.
Or use SHC's classes, they look good too. Although I'm not sure if you can make a machine gun with them.
SHC, maybe you could post a machine gun example using your library?
25  Game Development / Newbie & Debugging Questions / Re: Can't figure out how to do Java Audio properly. on: 2013-10-05 05:13:55
But yeah, loading the sound files on the fly isn't going to work. It would cause too much latency. Like Jeremy said, for a machine gun (and my game happens to have an Assault Rifle), it just won't work. My sounds are used too often.

Then do it like this

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  
public class AudioData {
    byte[] data;
    AudioFormat format;
    AudioData(String fileName) {
       try {
           AudioInputStream sounds = AudioSystem.getAudioInputStream(AudioData.class.getResource(fileName));
           format = sounds.getFormat();
           int size = sounds.available();
           data = new byte[size];
           sounds.read(data);
           sounds.close();
       } catch(Exception e) {
           e.printStackTrace();
       }
    }
    public Clip createClip() {
        try {
            final Clip clip = AudioSystem.getClip();
            clip.addLineListener(new LineListener() {
             public void update(LineEvent e) {
                 LineEvent.Type type = e.getType();
                 if(type == type.STOP) clip.close();
             }
            });
            clip.open(format, data, 0, data.length);
            return clip;
        } catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
}


and then you can make your machine gun with this code
1  
2  
3  
4  
5  
AudioData data = new AudioData("shoot.wav");
for(int i = 0; i < 100; i++) {
    data.createClip().start();
    Thread.sleep(100);
}


However of course it would be better to use Clip.loop for a machine gun but then the audio file may need some editing to make it sound correctly.
26  Game Development / Newbie & Debugging Questions / Re: Can't figure out how to do Java Audio properly. on: 2013-10-04 23:14:38
Except that if I close the sound, I have to reload the data, which isn't good for sounds that are played a lot. Also, when I said playing two sounds at the same time, I meant the SAME sound, not two different ones.

Of course you can also play the same sound twice at the same time.
1  
2  
play("shoot.wav");
play("shoot.wav");

You can also add a delay to get an echo effect.
1  
2  
3  
play("shoot.wav");
Thread.sleep(500);
play("shoot.wav");

That the data has to be loaded twice should not be a problem since the OS has a file cache. But if you think it's an issue you could load the wav files into byte arrays and then create clips from those.
27  Game Development / Newbie & Debugging Questions / Re: Can't figure out how to do Java Audio properly. on: 2013-10-04 22:11:51
Why do you create a Thread? Clips always play in the background, you don't need Threads for that.
The easiest way to play audio clips is this.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public static void play(String name) {
   try{
     AudioInputStream sounds = AudioSystem.getAudioInputStream(Sounds.class.getResource(name));
     final Clip clip = AudioSystem.getClip();
     clip.addLineListener(new LineListener() {
         public void update(LineEvent e) {
             LineEvent.Type type = e.getType();
             if(type == type.STOP) clip.close();
         }
     });
     clip.open(sounds);
     clip.start();
   } catch(Exception e){
       e.printStackTrace();
   }
}


If you want to play two clips at the same time you just call it twice.
e.g.
1  
2  
play("shoot.wav");
play("explosion.wav");
28  Game Development / Newbie & Debugging Questions / Re: Easiest way of playing sound and music? on: 2013-09-29 03:17:30
I changed your Sounds class. Now it should always play. However if you call play while the same clip is still playing it will be stopped and restarted. If you want an overlap i.e. the same clip playing twice at the same time, you need to create two clips from the same audio file.
I may have used more locking here than necessary but it seems to be working fine.

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  
import javax.sound.sampled.*;
import java.util.Hashtable;

public class Sounds {
   final static Hashtable<Clip, Boolean> isPlaying = new Hashtable<>();
   final static Hashtable<Clip, Object> locks = new Hashtable<>();
   
   public static Clip shooting= loadClip("/sound/shooting.wav");

   private static Clip loadClip(String path){
      try{
         AudioInputStream sounds = AudioSystem.getAudioInputStream(Sounds.class.getResource(path));
         final Clip clip = AudioSystem.getClip();
         isPlaying.put(clip, false);
         locks.put(clip, new Object());
         clip.open(sounds);
         clip.addLineListener(new LineListener() {
             public void update(LineEvent e) {
                 LineEvent.Type type = e.getType();
                 if(type == type.START) {
                     synchronized(locks.get(clip)) {
                         isPlaying.put(clip, false);
                     }
                 } else if(type == type.STOP) {
                     synchronized(locks.get(clip)) {
                         if(isPlaying.get(clip)) {
                             clip.setFramePosition(0);
                             clip.start();
                         }
                     }
                 }
             }
         });
         return clip;
      }catch(Exception e){
         e.printStackTrace();
         return null;
      }
   }
   
   public static void play(Clip clip){
      if(clip != null){
         try{
             synchronized(locks.get(clip)) {
                 isPlaying.put(clip, true);
                 clip.stop();
                 clip.setFramePosition(0);
                 clip.start();
             }
         }catch(Exception e){
            e.printStackTrace();
         }
      }
   }
   
   public static void stop(Clip clip){
      if(clip != null){
         clip.stop();
      }
   }
}


edit:
There is a much easier way to do it. Just create a new Clip every time you play a sound. That way you also get overlapping sound. You can replace the entire Sounds class with just this one method.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public static void play(String path) {
   try{
     AudioInputStream sounds = AudioSystem.getAudioInputStream(Sounds.class.getResource(path));
     final Clip clip = AudioSystem.getClip();
     clip.addLineListener(new LineListener() {
         public void update(LineEvent e) {
             LineEvent.Type type = e.getType();
             if(type == type.STOP) clip.close();
         }
     });
     clip.open(sounds);
     clip.start();
   } catch(Exception e){
       e.printStackTrace();
   }
}

And then just call it with the name of your sound file.
29  Java Game APIs & Engines / Java 2D / Re: Design Pattern For Game Settings on: 2013-09-28 02:19:00
Now here is another example. This time it uses a completely different strategy. It stores all settings in a HashMap. Also it uses the Observer pattern.

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.io.*;
import java.util.*;
import java.awt.Dimension;

abstract class SettingListener<T> {
    String name;
   
    SettingListener(String name) {
        this.name = name;
    }
   
    String getName() {
        return name;
    }
   
    abstract void reactToChange(T oldValue, T newValue);
}

class GameFrame {
    Game game;
    GameFrame(Game game) {
        this.game = game;
        game.changeSetting("resolutionX", 800.0);
        game.changeSetting("resolutionY", 600.0);
        game.changeSetting("isFullScreen", false);
        game.changeSetting("fps", 60.0);
    }
}

class AudioSystem {
    Game game;
    AudioSystem(Game game) {
        this.game = game;
        game.changeSetting("isSoundOn", true);
        game.changeSetting("volume", 100.0);
        game.addSettingListener(new SettingListener<Double>("volume") {
            void reactToChange(Double oldValue, Double newValue) {
                System.out.println("volume was changed from " + oldValue + " to " + newValue);
            }
        });
    }
}

class Game {
    Map<String, Object> settings = new HashMap<>();
    List<SettingListener> settingListeners = new ArrayList<>();
   
    void addSettingListener(SettingListener listener) {
        settingListeners.add(listener);
    }
   
    Object getSetting(String name) {
        return settings.get(name);
    }
   
    @SuppressWarnings("unchecked")
    void changeSetting(String name, Object value) {
        Object oldValue = settings.get(name);
        settings.put(name, value);
        for(SettingListener listener: settingListeners) {
            if(listener.getName().equals(name))
                listener.reactToChange(oldValue, value);
        }
    }
   
    GameFrame gameFrame = new GameFrame(this);
    AudioSystem audioSystem = new AudioSystem(this);
   
    GameFrame getGameFrame() {
        return gameFrame;
    }
    AudioSystem getAudioSystem() {
        return audioSystem;
    }
   
    public static void main(String[] args) {
        Game game = new Game();
        Tools.writeSettings("config.txt", game);
        game.changeSetting("fps", 100.0);
        game.changeSetting("volume", 50.0);
        System.out.println(game.getSetting("fps"));
        System.out.println(game.getSetting("volume"));
        Tools.readSettings("config.txt", game);
        System.out.println(game.getSetting("fps"));
        System.out.println(game.getSetting("volume"));
    }
}

class Tools {
    static void writeSettings(String fileName, Game game) {
        PrintWriter out = null;
        try {
            out = new PrintWriter(fileName);
            for(String name: game.settings.keySet()) {
                Object value = game.settings.get(name);
                out.println(name + ": " + value);
            }
            out.flush();
            out.close();
        } catch(IOException e) {
            e.printStackTrace();
        } finally {
            if(out != null) out.close();
        }
    }
    static void readSettings(String fileName, Game game) {
        Scanner in = null;
        try {
            in = new Scanner(new File(fileName)).useDelimiter("\\s+|\\s*:\\s*");
            while(in.hasNext()) {
                String name = in.next();
                if(name.length() == 0) continue;
                String value = in.next();
                Double doubleValue = null;
                try {
                    doubleValue = Double.valueOf(value);
                } catch(NumberFormatException e) {
                }
                if(doubleValue != null) game.changeSetting(name, doubleValue);
                else game.changeSetting(name, value);
            }
        } catch(IOException e) {
            e.printStackTrace();
        } finally {
            if(in != null) in.close();
        }
    }
}
30  Java Game APIs & Engines / Java 2D / Re: Design Pattern For Game Settings on: 2013-09-28 01:33:29
Ok, here is the most simple solution I could think of.
Everything is hard coded. Not recommended for large projects, but for simple games it should be sufficient.
This example program can be compiled and run as it is.

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  
import java.io.*;
import java.awt.Dimension;
import java.util.Scanner;

class GameFrame {
    Dimension resolution = new Dimension(800,600);
    boolean isFullScreen = false;
    double fps = 60;
   
    Dimension getResolution() {
        return resolution;
    }
    void setResolution(Dimension resolution) {
        this.resolution = resolution;
    }
    boolean isFullScreen() {
        return isFullScreen;
    }
    void setFullScreen(boolean isFullScreen) {
        this.isFullScreen = isFullScreen;
    }
    double getFPS() {
        return fps;
    }
    void setFPS(double fps) {
        this.fps = fps;
    }
}

class AudioSystem {
    boolean isSoundOn = true;
    double volume = 100;
   
    boolean isSoundOn() {
        return isSoundOn;
    }
    void setSoundOn(boolean isSoundOn) {
        this.isSoundOn = isSoundOn;
    }
    double getVolume() {
        return volume;
    }
    void setVolume(double volume) {
        this.volume = volume;
    }
}

class Game {
    GameFrame gameFrame = new GameFrame();
    AudioSystem audioSystem = new AudioSystem();
   
    GameFrame getGameFrame() {
        return gameFrame;
    }
    AudioSystem getAudioSystem() {
        return audioSystem;
    }
   
    public static void main(String[] args) {
        Game game = new Game();
        Tools.writeSettings("config.txt", game);
        game.getGameFrame().setFPS(100);
        game.getAudioSystem().setVolume(50);
        System.out.println(game.getGameFrame().getFPS());
        System.out.println(game.getAudioSystem().getVolume());
        Tools.readSettings("config.txt", game);
        System.out.println(game.getGameFrame().getFPS());
        System.out.println(game.getAudioSystem().getVolume());
    }
}

class Tools {
    static void writeSettings(String fileName, Game game) {
        PrintWriter out = null;
        try {
            out = new PrintWriter(fileName);
            GameFrame gameFrame = game.getGameFrame();
            AudioSystem audioSystem = game.getAudioSystem();
            Dimension resolution = gameFrame.getResolution();
            out.println("resolution: " + resolution.width + ", " + resolution.height);
            out.println("isFullScreen: " + gameFrame.isFullScreen());
            out.println("fps: " + gameFrame.getFPS());
            out.println("isSoundOn: " + audioSystem.isSoundOn());
            out.println("volume: " + audioSystem.getVolume());
            out.flush();
            out.close();
        } catch(IOException e) {
            e.printStackTrace();
        } finally {
            if(out != null) out.close();
        }
    }
    static void readSettings(String fileName, Game game) {
        Scanner in = null;
        try {
            in = new Scanner(new File(fileName)).useDelimiter("\\s+|\\s*:\\s*|\\s*,\\s*");
            GameFrame gameFrame = game.getGameFrame();
            AudioSystem audioSystem = game.getAudioSystem();
            while(in.hasNext()) {
                String str = in.next();
                if(str.length() == 0) continue;
                if(str.equals("resolution")) gameFrame.setResolution(
                    new Dimension(Integer.parseInt(in.next()), Integer.parseInt(in.next())));
                else if(str.equals("isFullScreen")) gameFrame.setFullScreen(
                    Boolean.parseBoolean(in.next()));
                else if(str.equals("fps")) gameFrame.setFPS(
                    Double.parseDouble(in.next()));
                else if(str.equals("isSoundOn")) audioSystem.setSoundOn(
                    Boolean.parseBoolean(in.next()));
                else if(str.equals("volume")) audioSystem.setVolume(
                    Double.parseDouble(in.next()));
            }
        } catch(IOException e) {
            e.printStackTrace();
        } finally {
            if(in != null) in.close();
        }
    }
}
Pages: [1] 2 3 4
 

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

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (31 views)
2014-04-15 23:08:23

BurntPizza (28 views)
2014-04-15 08:46:01

UprightPath (43 views)
2014-04-14 22:39:50

UprightPath (26 views)
2014-04-14 22:35:47

Porlus (42 views)
2014-04-14 20:48:38

tom_mai78101 (64 views)
2014-04-10 09:04:31

BurntPizza (124 views)
2014-04-09 04:06:04

tom_mai78101 (224 views)
2014-04-05 18:34:39

trollwarrior1 (190 views)
2014-04-04 17:06:45

CJLetsGame (198 views)
2014-04-01 07:16:10
List of Learning Resources
by SHC
2014-04-18 08:17:39

List of Learning Resources
by Longarmx
2014-04-08 08:14:44

Good Examples
by matheus23
2014-04-05 18:51:37

Good Examples
by Grunnt
2014-04-03 20:48:46

Good Examples
by Grunnt
2014-04-03 20:48:37

Good Examples
by matheus23
2014-04-01 23:40:51

Good Examples
by matheus23
2014-04-01 23:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 20:22:30
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!