I bumped into a strange bug the other day. I found a way to fix it, but I have no idea why it works and I’m wondering if someone can explain it.
Here is the situation. I built a small set of classes to aid in my game development. One class, “SoundFactory”, has only static methods for loading and playing Clips. It also has two static fields for HashMaps. I never intended to create an instance of SoundFactory.
The first game that I wrote with the help of SoundFactory worked fine, but the next one did not. The code simply stopped executing somewhere soon after a call to a SoundFactory static method. Subsequent Game runs always stopped at the same place, until I commented out a few miscellaneous instantiations. Then the code consistently stopped at a different spot. The “stopping spots” always included an instantiation. No errors were thrown. Code just stopped executing.
So why did Game1 work and not Game2? I commented out code until I finally found the difference. Game1 had the following field declaration/assignment:
Color color = Color.BLUE;
When I added this seemingly irrelevant line as a field for Game2, the code ran fine.
Can anyone explain what I was doing wrong? I am guessing that I should abandon the whole static idea, but I am still curious about what the hang-up might be.
I code in 1.4 using Eclipse with my Mac, 10.3.
thanks,
josh