【问题标题】:How to make sound play only once on mouse over in Processing?如何在处理中只在鼠标悬停时播放一次声音?
【发布时间】:2019-12-12 23:42:41
【问题描述】:

当鼠标移到处理中的按钮上时,我正在尝试播放声音。目前,它正在一遍又一遍地播放,因为我的按钮类在 draw() 函数中。我理解为什么会发生这种情况,但我想不出一种只播放一次声音同时仍与我的overRect() 函数绑定的方法。

主要:

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;
     }
  }
}

【问题讨论】:

    标签: audio processing mouseover


    【解决方案1】:

    想通了。 初始化 int i = 0。当鼠标悬停在按钮上时,在 i

    编辑 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;
         }
      }
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用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
      }
      

      您还可以查看您正在使用的声音库,它可能有一个功能可以告诉您声音是否已经在播放,您可以以相同的方式使用它。

      【讨论】:

      • 这是有道理的。在您发布之前,我想到了与您的想法类似的东西(但效率稍低)。 state 变量实际上控制了这个按钮的游戏状态,我只是没有包含整个代码,因为我遇到的问题只是声音。谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-10
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 2015-09-16
      相关资源
      最近更新 更多