碰撞声只有一次

时间:2015-05-27 07:18:46

标签: triggers processing

我有以下代码来检查代理冲突。 我想在它们开始碰撞时只发射一次MIDI信息。

到目前为止我已经得到了这个。

void draw(){
        //Loop through people, and check collision, then play note, if intersecting
        for(int i=0;i<people.size();i++){
          Person p = people.get(i);
          p.collide(people,collisions);
          p.triggerMidi();
          p.run();
        }
  }



  public void collide(ArrayList<Person> people, ArrayList<Person> connections) {
   for(Person other : people) {
   if (other != this) {
    if (this.collide(other)) {
      this.isIntersecting=true;
     //connections.add(other); // when a collision is found, add it to a list for later use.
    }
  }
}
}

void triggerMidi(){
     if(!hasPlayed && this.isIntersecting==true){
      MIDI.sendNoteOn(channel, agentNote, 127); 
      delay(200);
      MIDI.sendNoteOff(channel,agentNote, 127);
      hasPlayed=true;
   }
}

这适用于在碰撞开始时仅播放一次声音。 但是如何在另一次碰撞开始时让它再次发挥。

显然我必须将hasPlayed设置为false。 但在哪里?

当我在碰撞循环中将其设置为false时,声音会播放一百万次。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

首先,您可能不应该从绘图线程中调用delay()。这将导致你的草图变得迟钝和反应迟钝。相反,您可能希望将声音播放到不同的线程上。

然后,为了回答你原来的问题 - 你知道这个音符有多长时间吗?如果是这样,只记录音符开始的时间,然后使用该时间检查已用时间。 millis()函数可能会派上用场。当经过的时间大于笔记的持续时间时,您可以将hasPlayed设置为假。

相关问题