If you take a look at my latest commit to the Water Drum Game, you’ll see that I added a few features! Notably, when in free play mode, the drums now make noise. Not only that, but they also “bobble” a little.
Hard to tell in this photo, but the middle water drum is pressed in.
The code for sound was rather easy, it goes like this:
if (hasDrummed) {
for (int i = 0; i < numDrums; i++) {
if (waterDrums[i].getPosition().contains
(mouseWorldPosition)) {switch (i) {
case 4:
drumFive.play();
break;case 3:
drumFour.play();
break;case 2:
drumThree.play();
break;case 1:
drumTwo.play();
break;default:
drumOne.play();
break;
}hasDrummed = false;
acceptInput = false;
waterDrums[i].struck();}
}
}
Earlier in the code, the “hasDrummed” bool is set to true when you click the mouse left button. So, in this code, it checks to see if the mouse pointer was inside the sprite for each of the water drums. If the mouse pointer is inside one of the water drums, it uses that drum number to play the appropriate sound. Remember, while there are 5 drums, they are labeled 0-4, since 0 counts as the first number.
As an aside, since the catch all, or default, is drum one sounds, the number of drums is scale-able, but it would mean all extra drums just have the same sound as the first drum.
The code for the drums to “bobble” were a bit more complex. But, I plan to update that and add more “bobble” motions in another commit and cover it specifically in another post.
Linux – keep it simple.