使用鼠标按下类

时间:2016-04-22 19:45:21

标签: processing

我制作了这个音板,这是我的第一个项目,我很高兴我到目前为止:)

但是,我正在尝试将mousePressed用于某个类并且它似乎无法正常工作,我希望它能够在单击一次时播放并在单击两次时停止。此时,当您将鼠标悬停在我的音板上的按钮上时,它会自动播放声音,直到您将其移动到另一个按钮。

有人可以向我解释这个问题吗?

class sound{


  float x;
  float y;

  public sound(float x, float y) {
    this.x = x;
    this.y = y;
  } 
  void run()
  {
    draw();
    mousePressed();
  }

  void draw() {
    rect(x, y, 99, 99);
    fill(100, 10, 10);
  }
  void mousePressed(){
    if (mouseX > 1 && mouseX <  99  
    && mouseY > 1 && mouseY <  99) {
    s2.play();
    image(p7, 1, 1);
  } else {
    s2.pause();
    s2.rewind();
  }

  if (mouseX > 100 && mouseX < 199 
    && mouseY > 1 && mouseY < 99) {
    player.play();
    image(p9, 100, 1);
  } else {
    player.pause();
    player.rewind();
  }
  if (mouseX > 200 && mouseX <300
    && mouseY > 1 && mouseY < 99) {
    s7.play();
    image(p6, 200, 1);
  } else {
    s7.pause();
    s7.rewind();
  }

  if (mouseX > 1 && mouseX < 99
    && mouseY > 100 && mouseY < 199) {
    s8.play();
    image(p8, 1, 100);
  } else {
    s8.pause();
    s8.rewind();
  }

  if (mouseX > 100 && mouseX < 199
    && mouseY > 100 && mouseY < 199) {
    s3.play();
    image(p2, 100, 100);
  } else
  {
    s3.pause();
    s3.rewind();
  }

  if (mouseX > 200 && mouseX < 300
    && mouseY > 100 && mouseY < 199) {
    s6.play();
    image(p5, 200, 100);
  } else
  {
    s6.pause();
    s6.rewind();
  }

  if (mouseX > 1 && mouseX < 99
    && mouseY > 200 && mouseY < 299) {
    s1.play();
    image(p1, 1, 200);
  } else
  {
    s1.pause();
    s1.rewind();
  }
  if (mouseX > 100 && mouseX < 199 
    && mouseY > 200 & mouseY < 299) {
    s4.play();
    image(p3, 100, 200);
  } else
  {
    s4.pause();
    s4.rewind();
  }
  if (mouseX> 200 && mouseX < 299
    && mouseY > 200 && mouseY < 299) {
    s5.play();
    image(p4, 200, 200);
  } else
  {
    s5.pause();
    s5.rewind();
  }

  }

}


void setup()
{
  size(300, 300);
  minim = new Minim(this);
  player = minim.loadFile("deadmau5.mp3");
  s1 = minim.loadFile("biggie.mp3");
  s2 = minim.loadFile("crush.mp3");
  s3 = minim.loadFile("ine.mp3");
  s4 = minim.loadFile("geazy.mp3");
  s5 = minim.loadFile("lil.mp3");
  s6 = minim.loadFile("manson.mp3");
  s7 = minim.loadFile("ochoa.mp3");
  s8 = minim.loadFile("tiesto.mp3");

  soundboard =loadImage("sb.png");
  p1 = loadImage("biggie.jpg");
  p2 = loadImage("dusky.jpg");
  p3 = loadImage("geazy.jpg");
  p4 = loadImage("lil.jpg");
  p5 = loadImage("manson.JPG");
  p6 = loadImage("ochoa.jpg");
  p7 = loadImage("pvd.jpg");
  p8 = loadImage("tiesto.jpg");
  p9 = loadImage("deadmouse.jpg");
  sound1 = new sound(1, 1);
  sound2 = new sound(101, 1);
  sound3 = new sound(201, 1);
  sound4 = new sound(1, 101);
  sound5 = new sound(101, 101);
  sound6 = new sound(201, 101);
  sound7 = new sound(1, 201);
  sound8 = new sound(101, 201);
  sound9 = new sound(201, 201);
}

void draw()
{
  image(soundboard, 1, CENTER);
  sound1.run();
  sound2.run();
  sound3.run();
  sound4.run();
  sound5.run();
  sound6.run();
  sound7.run();
  sound8.run();
  sound9.run();
  sound1.mousePressed();
    sound2.mousePressed();
  sound3.mousePressed();
  sound4.mousePressed();
  sound5.mousePressed();
  sound6.mousePressed();
  sound7.mousePressed();
  sound8.mousePressed();
  sound9.mousePressed();

}

void mousePressed() {
} 

2 个答案:

答案 0 :(得分:0)

您正在从草图的mousePressed()功能调用Sound课程的draw()功能。您实际上并未检查用户是否按下了鼠标。

您可能只想将这些调用移到草图的mousePressed()函数中。

答案 1 :(得分:0)

void mousePressed(){
    if (mouseX > 1 && mouseX <  99  && mouseY > 1 && mouseY <  99) {
         s2.play();
         image(p7, 1, 1);
     }else {
         s2.pause();
         s2.rewind();
       }

我认为这是问题,你检查你的鼠标位置在哪里,如果不在那个位置,声音会暂停。