Audio 如何使声音在处理过程中只在鼠标上播放一次?

Audio 如何使声音在处理过程中只在鼠标上播放一次?,audio,processing,mouseover,Audio,Processing,Mouseover,当鼠标在处理过程中移动到按钮上时,我试图播放声音。目前,由于我的button类位于draw()函数中,因此它正在反复播放。我理解为什么会发生这种情况,但我想不出一种方法,在仍然绑定到我的overRect()函数的情况下,只播放一次声音 主要内容: 导入处理。声音。*; 播放按钮; 声音文件鼠标翻转; 声音文件点击声音; 颜色白色=颜色(255); 颜色灰色=颜色(241,241,241); 普冯·维达那博尔德; 无效设置() { 规模(7201280); 文本对齐(中心); 矩形模式(中心);

当鼠标在处理过程中移动到按钮上时,我试图播放声音。目前,由于我的button类位于
draw()
函数中,因此它正在反复播放。我理解为什么会发生这种情况,但我想不出一种方法,在仍然绑定到我的
overRect()
函数的情况下,只播放一次声音

主要内容:

导入处理。声音。*;
播放按钮;
声音文件鼠标翻转;
声音文件点击声音;
颜色白色=颜色(255);
颜色灰色=颜色(241,241,241);
普冯·维达那博尔德;
无效设置()
{
规模(7201280);
文本对齐(中心);
矩形模式(中心);
verdanabold=createFont(“Verdana Bold”,60,真);
playButton=新的playButton();
mouseOverSound=新的声音文件(此文件为“mouseover.wav”);
clickSound=新的声音文件(这是“click.mp3”);
}
作废提款()
{
背景(灰色);
playButton.display();//绘制播放按钮
}
播放按钮类:

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;
     }
  }
}
类播放按钮//播放按钮
{
float rectX=宽度/2;//按钮x位置
float rectY=高度/4;//按钮y位置
int rectWidth=275;//按钮宽度
int rectHeight=75;//按钮高度
boolean rectOver=false;//确定鼠标是否在按钮上方的布尔值
void display()//绘制播放按钮并控制其功能
{
更新(mouseX,mouseY);
if(rectOver)//鼠标悬停时控制按钮颜色
{
填充(白色);
mouseOverSound.play();//在声音上播放鼠标
}
其他的
{
填充(灰色);
}
冲程重量(5);//按钮
笔划(黑色);
rect(rectX、rectY、rectWidth、rectHeight);
textFont(verdanabold,48);//按钮文本
填充(黑色);
文本(“播放”,矩形,矩形+15);
if(mousePressed&&rectOver)//如果鼠标悬停并单击,则更改为状态1
{
状态=1;
clickSound.play();//播放单击声音
}
}
void update(float x,float y)//使用overlect()确定鼠标是否在按钮上方,并相应地更改布尔rectOver
{
if(过度选择(矩形、矩形、矩形宽度、矩形高度))
{
rectOver=true;
}
其他的
{
rectOver=false;
}
}
布尔覆盖(float-rectX、float-rectY、int-rectWidth、int-rectHeight)//比较鼠标位置和按钮位置,如果=
{

如果(mouseX>=rectX rectWidth/2&&mouseX=rectY rectHeight/2&&mouseY,您可以使用一个
布尔
变量来指示声音是否已经播放。类似如下:

if(鼠标按下&&rectOver&&alreadyPlaying)
{
alreadyPlaying=true;
状态=1;
clickSound.play();//播放单击声音
}
事实上,您可能可以使用
state
变量:

if(鼠标按下&&rectOver&&state!=1)
{
状态=1;
clickSound.play();//播放单击声音
}
你也可以查看你正在使用的声音库,它可能有一个函数,告诉你声音是否已经在播放,你可以用同样的方式使用它。

找到了答案。 初始化int i=0。当鼠标位于按钮上方时,在i<1时播放声音,然后在While循环中将i增加1,使其停止播放。当鼠标未位于按钮上方时,i被设置回0

编辑的播放按钮类:

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;
     }
  }
}
class HowToButton//HowToButton
{
float rectX=宽度/2;//按钮x位置
float rectY=高度/8;//按钮y位置
int rectWidth=275;//按钮宽度
int rectHeight=75;//按钮高度
boolean rectOver=false;//确定鼠标是否在按钮上方的布尔值
int i=0;
void display()//绘制如何单击按钮并控制其功能
{
更新(mouseX,mouseY);
if(rectOver)//鼠标悬停时控制按钮颜色
{
填充(白色);
while(i<1)//在i<1时播放声音
{
mouseOverSound.play();//在声音上播放鼠标
i++;//因此声音只播放一次
}
}
其他的
{
填充(灰色);
i=0;//当鼠标离开按钮边界时,将i设置回0
}
冲程重量(5);//按钮
笔划(黑色);
rect(rectX、rectY、rectWidth、rectHeight);
textFont(verdanabold,48);//按钮文本
填充(黑色);
文本(“如何”,rectX,rectY+15);
if(mousePressed&&rectOver)//如果鼠标悬停并单击,则更改为状态2
{
状态=2;
clickSound.play();//播放单击声音
}
}
void update(float x,float y)//使用overlect()确定鼠标是否在按钮上方,并相应地更改布尔rectOver
{
if(过度选择(矩形、矩形、矩形宽度、矩形高度))
{
rectOver=true;
}
其他的
{
rectOver=false;
}
}
布尔覆盖(float-rectX、float-rectY、int-rectWidth、int-rectHeight)//比较鼠标位置和按钮位置,如果=
{

如果(mouseX>=rectX rectWidth/2&&mouseX=rectY rectHeight/2&&mouseY),这是有道理的。我想到了与您的想法类似的东西(但效率稍低)就在你发布之前。state变量实际上控制着这个按钮的游戏状态,我只是没有包括全部代码,因为我遇到的问题只是声音。谢谢!