Could anyone help me rearrange my code so that mousepressed presses all four ellipses?

edited November 2016 in Library Questions

The assignment is to create four ellipses that each have a different sound, and to make the code run so that each ellipse plays its sound when clicked (mousepressed). I have managed to get the top two ellipses to work; however, I cannot get the other two to work so I would really appreciate some help. (Down at the very end with my if/else if/else statements).

    // import Minim
     import ddf.minim.*;

     Minim minim;
     AudioSample d1;
     AudioSample d2;
     AudioSample d3;
     AudioSample d4;

     AudioOutput out;

     // track when a drum has been struck
     boolean drum1struck;
     boolean drum2struck;
     boolean drum3struck;
     boolean drum4struck;


     void setup() {
       // initialize the screen
       size(400, 400);
       smooth();

       // initialize sound
       minim = new Minim(this);
     out = minim.getLineOut();

       d1 = minim.loadSample("bongo1.wav");
       d2 = minim.loadSample("bongo7.wav");
       d3 = minim.loadSample("tom1.wav");
       d4 = minim.loadSample("tom2.wav");

       // set boolean variables to initialize the graphics
       drum1struck = false;
       drum2struck = false;
       drum3struck = false;
       drum4struck = false;

     }

     void draw() {
        background(255);

       // draw the drums: if a drum has just been struck
       // then fill its ellipse with color as visual feedback for the user

       // drum 1
       if (drum1struck == true) {
         fill(255,0,0);
         drum1struck = false;
       } 
       else 
       {
         fill(255);
       }
       ellipse(50, 55, 100, 100);

       // drum 2
       if (drum2struck == true) {
         fill(255,0,0);
         drum2struck = false;
       } else {
         fill(255);
       }
       ellipse(160, 55, 100, 100);

       //drum 3
       if (drum3struck == true) {
         fill(255,0,0);
         drum3struck = false;
       } 
       else {
         fill(255);
       }
       ellipse(50, 165, 100, 100);

       //drum 4
       if (drum4struck == true) {
         fill(255,0,0);
         drum4struck = false;
       } else {
         fill(255);
       }
       ellipse(160, 165, 100, 100);


     }
     void keyPressed() {
       if (key == 'a' || key == 'A') {   //note you may have to retype the
         drum1struck = true;             //single quote marks after copying
         d1.trigger();
       }
       else if (key == 'b' || key == 'B') {
         drum2struck = true;
         d2.trigger();
       }
       else if (key == 'c' || key == 'C') {
         drum3struck = true;
         d3.trigger();
       }
       else if (key == 'd' || key == 'D') {
         drum4struck = true;
         d4.trigger();
       }
     }

     void mousePressed() {
       if (mouseX<105)
       {
            d1.trigger();
            //set Boolean for drum1 to true
            drum1struck = true;
       }
       else if (mouseX>105)
            {d2.trigger();
            //set Boolean for drum1 to true
           drum2struck = true;
       }
      else if (mouseY<-200)
       {
            d3.trigger();
            //set Boolean for drum1 to true
            drum3struck = true;
       }
       else  {
            d4.trigger();
            //set Boolean for drum1 to true
            drum4struck = true;
     }
     }
Tagged:

Answers

  • Please format your code. Edit your post, select your code and hit ctrl+o.

    Also you need to provided a better description of your problem. It is unclear what you mean.

    Kf

  • edited November 2016

    Hope this helps

  • Use the dist function: https://processing.org/reference/dist_.html

    You know where your ellipses are. For example using drum1:

    int radius=100;
    ellipse(50, 55, radius, radius);
    

    Then in mousePressed do the following:

    if(dist,50+radius,55+radius,mouseX,mouseY)<radius){
      //
      //  You are inside the ellipse
      //
    }
    

    Notice you are working with the default ellipseMode. When you use the dist function, you need to check the mouse coordinates with respect to the center of the ellipse. That is the reason why I introduce the radius in the dist calculation, to work with the center of the ellipse. Check the next link for more info:

    https://processing.org/reference/ellipseMode_.html

    Kf

Sign In or Register to comment.