Creating a soundboard. Need help!

``Hello everyone,

I've been trying to create a soundboard that is made up of 9 buttons. I've created a Button class, with two functions (display and play):

function Button(){

      //Displays the button. This function takes two arguments
      //x and y, and uses them as co-ordinates for the button
      this.display= function(x,y){
      this.xpos=x;
      this.ypos=y;
      noStroke();
      fill(255,0,0);
      rect(this.xpos,this.ypos,60,60);
      }

      //This function checks if the button has been pressed.
      //It checks if the mouse is within the boundary of the button being pressed.

      this.press= function(){

        if(mouseIsPressed){
          if(mouseX>this.xpos&&mouseX<this.xpos+60&&mouseY>this.ypos&&mouseY<this.ypos+60){
            print();
            sound[0].play();
            }
        }
      }
    }

I've also used for loops to display the buttons. The for loop is in setup, and the buttons.play(); function is loaded into the draw function:

        //For loop to create buttons in setup
         for(var y=100; y<300; y+=90){
            for(var x=100; x<300; x+=90){
              for(var p=0;p<buttons.length;p++){
                buttons[p].display(x,y);
                sound[p].setVolume(0.6);
                sound[p].playMode('restart')

                }
            }
          }

        //Creates the button.press() function, on top of the buttons
         for(var y=100; y<300; y+=90){
            for(var x=100; x<300; x+=90){
              for(var i=0;i<buttons.length;i++){
                buttons[i].press();

                }
            }
          }

The problem is that it only plays the sound on the last button, instead of any of the other buttons I click on. I'm completely confused as to why this isn't working!

Tagged:

Answers

  • From the way you are doing it I see you are doing it wrong

    I can't see where you use new

    The idea is that you store x,y in the class (the constructor is doing this when called with new)

    Then you don't need to call display with numbers, because then x,y are known in the class

    X,y must be in the class and be defined

    Then the check for press would also work

Sign In or Register to comment.