简体   繁体   中英

Return a true statement back to false in Processing

I am trying to make an audio sample play when Kinect detects an object in a rectangular region. The problem is that I've currently set it up so that it plays the sample once, then changes the region to not play another sample if I move outside the region.

How do I make it so that it plays it every time I move in the region and reset it after I move out to toggle again?

I've tried using separate Boolean functions like:

else if (region1 = true){ 
region1 = false;
}

but that doesn't seem to work. I think that I need to work within that function, rather than making a new one to reset it to false.

Heres what I've got so far:

boolean region1 = false;

       if (blob.getRect().intersects(screenRects.get(0)) && !region1){
         println("region1-"+millis());
           String filename = dataPath("sample.wav");
               SamplePlayer sp = new SamplePlayer(ac, SampleManager.sample(filename));
               ac.out.addInput(sp);
         region1 = true;
       } 

Change else if(region1 = true) to else if(region1 == true) . = is an assignment operator.

Assuming you implemented the suggestions in the comments and your code now looks like this:

   if (blob.getRect().intersects(screenRects.get(0)) && !region1){
     println("region1-"+millis());
       String filename = dataPath("sample.wav");
           SamplePlayer sp = new SamplePlayer(ac, SampleManager.sample(filename));
           ac.out.addInput(sp);
     region1 = true;
   } 
   else if (region1 == true){ 
     region1 = false;
   }

What happens is that when an object in the region is detected, audio plays and region1 is set true. The very next frame, the else if runs and region1 is set to false again. This means that the frame after that, if the object is still present, the audio plays again.
The region1 -variable means 'object has entered region', and should only be reset if there is no object present in the region. Try this:

boolean isObjectInRegion;

   isObjectInRegion= blob.getRect().intersects(screenRects.get(0));
   if (isObjectInRegion && !region1){
     println("region1-"+millis());
       String filename = dataPath("sample.wav");
           SamplePlayer sp = new SamplePlayer(ac, SampleManager.sample(filename));
           ac.out.addInput(sp);
     region1 = true;
   } 
   else if (!isObjectInRegion && region1 == true){ 
     region1 = false;
   }

Meaning: if there was an object in the region, but not anymore, then reset region1 . It will then trigger the audio when the object re-enters the region.

Could it be that this whole thing happens because of multi-threading? If the changes you make and the querying take place in two different threads, you'll have to take more precautions. eg define the boolean you check as volatile .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM