I’m working on my first 2D game. It is more a puzzle than an action game–tiles are being dragged and dropped, and one is trying to achieve a certain objective pattern.
When the puzzle is completed, I launch a visual animation (glows for the various tiles) and some sound effects. SOMETIMES, the performance stutters. I reopened the program today, and didn’t recreate the problem in a dozen tries. But it sure was happening last night, and has been for a while.
The timing of the visuals is handled by a pre-exising util.Timer (my game loop timer) which does its thing by calling code which increments indexes into the arrays of graphics, then calling a repaint(). This code is “launched” by flipping a boolean. (The “glow cycle” references a premade set of graphics with varying alpha values at a slightly modified radial gradient.)
The timing of the sounds is handled by a freshly made util.Timer, where I create, on the fly a set of RemindTasks that each pop an index off of an array, and send a call to play the referenced object. Each of the sounds will have already been played once, prior to this call. But I haven’t found a way to “index” into an array of sounds like I do with graphics, that allows a single WAV to be played while overlapping. So new sounds are created on the fly from the resource files.
Mostly this works just fine, but sometimes there is a jerkiness. I’ve seen various things written about problems with util.Timers and the Java.sound package, and am wondering if these are due to inherent limitations (related to garbage collection or time needed for loading my sound files), or to complexities in how these items have to be handled in order to use them correctly.
As I write this, it occurs to me maybe I don’t need the second timer for launching the sounds as a sequence. I guess I could make the “run-once” remind tasks on the “game loop timer” and avoid the cost of making a new timer. Also, I might be able to make a special case for these sounds and eliminate situations where an overlap might occur, thus allowing for preloading them…
How difficult, in general is it to switch to another sound library? What is recommended? Is the reason “easier programming” (a great reason!) or is there a performance or reliability pickup in how sounds are played back by the JVM?
Are there other Timer() objects that run more reliably? I know there is a Swing.Timer, but I don’t know how it differs in terms of performance and reliability with the util.Timer.
Thanks for input! If any of the above needs to be supplemented by code examples, you’ll let me know, yes? I’m also working at trying to get an “in progress” Applet online. It is complicated as it involves loading a puzzle (the puzzles are beyond my capability to generate automatically) and some problem situations only arise upon completion (so I’ll also need the instructions to be up and a way of showing/revealing the solutions). Hah. You get what seems to be 80% done, and then the last 20% takes 80% of the total project time. Yes?