Playing a sound once when you've highlighted an option with the mouse
in
Core Library Questions
•
1 year ago
- import ddf.minim.*;
- import ddf.minim.signals.*;
- import ddf.minim.analysis.*;
- import ddf.minim.effects.*;
- PFont menuFont;
- AudioPlayer mSound;
- Minim mSoundLoader;
- int mouseSound = -1;
- void setup ()
- {
- precacheSound();
- menuFont = loadFont("Perpetua-32.vlw");
- }
- void draw ()
- {
- selectableText("Test", 50, 50, 1);
- }
- boolean highlighted (float x, float y, float x2, float y2)
- {
- if (mouseX >= x && mouseX <= x2 && mouseY >= y && mouseY <= y2)
- return true;
- return false;
- }
- void selectableText (String words, float xPos, float yPos, int soundNum)
- {
- textFont(menuFont, 32);
- if (highlighted(xPos, yPos - textAscent(), xPos + textWidth(words), yPos + textDescent()))
- {
- fill(255);
- if (mouseSound != soundNum)
- {
- mouseSound = soundNum;
- mSound.play();
- }
- }
- else
- {
- fill(100);
- if (mouseSound == soundNum)
- {
- mouseSound = -1;
- }
- }
- text(words, xPos, yPos);
- }
- void precacheSound ()
- {
- mSoundLoader = new Minim(this);
- mSound = mSoundLoader.loadFile("highlight.wav");
- }
With this code, whenever I highlight a text option, it brightens up (via the fill()). However, I cannot get sound to play. It sometimes play when it's supposed to, and it sometimes doesn't. What am I doing wrong?
1