我正在尝试在“处理”中的鼠标移至按钮上方时播放声音。当前,它正在反复播放,因为我的按钮类在draw()
函数中。我知道为什么会发生这种情况,但是我想不出一种只播放一次声音而又仍然与我的overRect()
函数绑定在一起的方法。
main:
import processing.sound.*;
PlayButton playButton;
SoundFile mouseOverSound;
SoundFile clickSound;
color white = color(255);
color gray = color(241, 241, 241);
PFont verdanabold;
void setup()
{
size(720, 1280);
textAlign(CENTER);
rectMode(CENTER);
verdanabold = createFont("Verdana Bold", 60, true);
playButton = new PlayButton();
mouseOverSound = new SoundFile(this, "mouseover.wav");
clickSound = new SoundFile(this, "click.mp3");
}
void draw()
{
background(gray);
playButton.display(); // draw play button
}
playButton类:
class PlayButton // play button
{
float rectX = width/2; // button x position
float rectY = height-height/4; // button y position
int rectWidth = 275; // button width
int rectHeight = 75; // button height
boolean rectOver = false; // boolean determining if mouse is over button
void display() // draws play button and controls its function
{
update(mouseX, mouseY);
if(rectOver) // controls button color when mouse over
{
fill(white);
mouseOverSound.play(); // play mouse over sound
}
else
{
fill(gray);
}
strokeWeight(5); // button
stroke(black);
rect(rectX, rectY, rectWidth, rectHeight);
textFont(verdanabold, 48); // button text
fill(black);
text("PLAY", rectX, rectY+15);
if(mousePressed && rectOver) // if mouse over and clicked, change to state 1
{
state = 1;
clickSound.play(); // play click sound
}
}
void update(float x, float y) // determines if mouse is over button using overRect(), changes boolean rectOver accordingly
{
if(overRect(rectX, rectY, rectWidth, rectHeight))
{
rectOver = true;
}
else
{
rectOver = false;
}
}
boolean overRect(float rectX, float rectY, int rectWidth, int rectHeight) // compares mouse pos to button pos and returns true if =
{
if(mouseX >= rectX-rectWidth/2 && mouseX <= rectX+rectWidth/2 && mouseY >= rectY-rectHeight/2 && mouseY <= rectY+rectHeight/2)
{
return true;
}
else
{
return false;
}
}
}
您可以使用boolean
变量来指示声音是否已经在播放。像这样的东西:
if(mousePressed && rectOver && !alreadyPlaying)
{
alreadyPlaying = true;
state = 1;
clickSound.play(); // play click sound
}
实际上,您可能可以使用state
变量:
if(mousePressed && rectOver && state != 1)
{
state = 1;
clickSound.play(); // play click sound
}
您还可以查看正在使用的声音库,它可能具有告诉您声音是否已经在播放的功能,您可以以相同的方式使用它。
想通了。初始化int i =0。当鼠标悬停在按钮上方时,在i <1时播放声音,然后在while循环内将i递增1,以便停止播放。当鼠标不在按钮上时,i设置回0。
编辑的playButton类:
class HowToButton // how to button
{
float rectX = width/2; // button x position
float rectY = height-height/8; // button y position
int rectWidth = 275; // button width
int rectHeight = 75; // button height
boolean rectOver = false; // boolean determining if mouse is over button
int i = 0;
void display() // draws how to button and controls its function
{
update(mouseX, mouseY);
if (rectOver) // controls button color when mouse over
{
fill(white);
while(i < 1) // play sound while i < 1
{
mouseOverSound.play(); // play mouse over sound
i++; // increment i so sound only plays once
}
}
else
{
fill(gray);
i = 0; // set i back to 0 when mouse leaves bounds of button
}
strokeWeight(5); // button
stroke(black);
rect(rectX, rectY, rectWidth, rectHeight);
textFont(verdanabold, 48); // button text
fill(black);
text("HOW TO", rectX, rectY+15);
if(mousePressed && rectOver) // if mouse over and clicked, change to state 2
{
state = 2;
clickSound.play(); // play click sound
}
}
void update(float x, float y) // determines if mouse is over button using overRect(), changes boolean rectOver accordingly
{
if(overRect(rectX, rectY, rectWidth, rectHeight))
{
rectOver = true;
}
else
{
rectOver = false;
}
}
boolean overRect(float rectX, float rectY, int rectWidth, int rectHeight) // compares mouse pos to button pos and returns true if =
{
if(mouseX >= rectX-rectWidth/2 && mouseX <= rectX+rectWidth/2 && mouseY >= rectY-rectHeight/2 && mouseY <= rectY+rectHeight/2)
{
return true;
}
else
{
return false;
}
}
}