We are about to switch to a new forum software. Until then we have removed the registration on this forum.
My Program is want to make a game When the leap motion move to the square will be playing music. But now I cannot play the music. So i want to know why and how to fix it. Thanks
Here is my code......
import com.onformative.leap.*;
import com.leapmotion.leap.Finger;
import ddf.minim.*;
LeapMotionP5 leap;
Minim minim;
/////////////////// here is + music
AudioPlayer Bongo;
AudioPlayer cello;
AudioPlayer drumset;
AudioPlayer flute;
AudioPlayer Guitar;
AudioPlayer handbells;
AudioPlayer harmonica;
AudioPlayer Harp;
AudioPlayer marimba;
AudioPlayer oboe;
AudioPlayer piano;
AudioPlayer saxphone;
AudioPlayer Trombone;
AudioPlayer trumpet;
AudioPlayer Tuba;
AudioPlayer violin;
void setup()
{
size(1280, 720, P3D);
minim = new Minim(this);
leap = new LeapMotionP5(this);
Bongo = minim.loadFile("Bongo.wav");
cello = minim.loadFile("cello.wav");
drumset = minim.loadFile("drumset.wav");
flute = minim.loadFile("flute.wav");
Guitar = minim.loadFile("Guitar.wav");
handbells = minim.loadFile("handbells.wav");
harmonica = minim.loadFile("harmonica.wav");
Harp = minim.loadFile("Harp.wav");
marimba = minim.loadFile("marimba.wav");
oboe = minim.loadFile("oboe.wav");
piano = minim.loadFile("piano.wav");
saxphone = minim.loadFile("saxphone.wav");
Trombone = minim.loadFile("Trombone.wav");
trumpet = minim.loadFile("trumpet.wav");
Tuba = minim.loadFile("Tuba.wav");
violin = minim.loadFile("violin.wav");
}
void draw() {
background(200, 200, 200);
text("Press r to reset.", 10, 20 );
fill (255);
for (Finger f : leap.getFingerList()) {
PVector fingerPos = leap.getTip(leap.getFinger(2));
if (fingerPos.x > 100 && fingerPos.x <180 && fingerPos.y > 100 && fingerPos.y < 180) {
Bongo.play();
Bongo.rewind( );
} else
Bongo.pause();
if (fingerPos.x > 650 && fingerPos.x <800 && fingerPos.y > 250 && fingerPos.y < 150) {
cello.play();
cello.rewind( );
} else
cello.pause();
if (fingerPos.x > 1050 && fingerPos.x <1200 && fingerPos.y > 250 && fingerPos.y < 150) {
drumset.play();
} else
drumset.pause();
if (fingerPos.x > 1450 && fingerPos.x <1600 && fingerPos.y > 250 && fingerPos.y < 150) {
flute.play();
} else
flute.pause();
if (fingerPos.x > 250 && fingerPos.x <400 && fingerPos.y > 350 && fingerPos.y < 150) {
Guitar.play();
} else
Guitar.pause();
if (fingerPos.x > 650 && fingerPos.x <800 && fingerPos.y > 350 && fingerPos.y < 150) {
handbells.play();
} else
handbells.pause();
if (fingerPos.x > 1050 && fingerPos.x <1200 && fingerPos.y > 350 && fingerPos.y < 150) {
harmonica.play();
} else
harmonica.pause();
if (fingerPos.x > 1450 && fingerPos.x <1600 && fingerPos.y > 350 && fingerPos.y < 150) {
Harp.play();
} else
Harp.pause();
if (fingerPos.x > 250 && fingerPos.x <400 && fingerPos.y > 600 && fingerPos.y < 150) {
marimba.play();
} else
marimba.pause();
if (fingerPos.x > 650 && fingerPos.x <800 && fingerPos.y > 600 && fingerPos.y < 150) {
oboe.play();
} else
oboe.pause();
if (fingerPos.x > 1050 && fingerPos.x <1200 && fingerPos.y > 600 && fingerPos.y < 150) {
piano.play();
} else
piano.pause();
if (fingerPos.x > 1450 && fingerPos.x <1600 && fingerPos.y > 600 && fingerPos.y < 150) {
saxphone.play();
} else
saxphone.pause();
if (fingerPos.x > 250 && fingerPos.x <400 && fingerPos.y > 850 && fingerPos.y < 150) {
Trombone.play();
} else
Trombone.pause();
if (fingerPos.x > 650 && fingerPos.x <800 && fingerPos.y > 850 && fingerPos.y < 150) {
trumpet.play();
} else
trumpet.pause();
if (fingerPos.x > 1050 && fingerPos.x <1200 && fingerPos.y > 850 && fingerPos.y < 150) {
Tuba.play();
} else
Tuba.pause();
if (fingerPos.x > 1450 && fingerPos.x <1600 && fingerPos.y > 850 && fingerPos.y < 150) {
violin.play();
} else
violin.pause();
stroke(0);
strokeWeight(5);
ellipse(fingerPos.x, fingerPos.y, 20, 20);
}
// Here is draw 16 square
stroke(0, 0, 0); // square
strokeWeight(5);
fill(255, 255, 255);
rect(250, 100, 150, 150);
stroke(0, 0, 0); // square
strokeWeight(5);
fill(255, 255, 255);
rect(650, 100, 150, 150);
stroke(0, 0, 0); // square
strokeWeight(5);
fill(255, 255, 255);
rect(1050, 100, 150, 150);
stroke(0, 0, 0); // square
strokeWeight(5);
fill(255, 255, 255);
rect(1450, 100, 150, 150);
//-----------------------------------------------------------
stroke(0, 0, 0); // square
strokeWeight(5);
fill(255, 255, 255);
rect(250, 350, 150, 150);
stroke(0, 0, 0); // square
strokeWeight(5);
fill(255, 255, 255);
rect(650, 350, 150, 150);
stroke(0, 0, 0); // square
strokeWeight(5);
fill(255, 255, 255);
rect(1050, 350, 150, 150);
stroke(0, 0, 0); // square
strokeWeight(5);
fill(255, 255, 255);
rect(1450, 350, 150, 150);
//-----------------------------------------------------------
stroke(0, 0, 0); // square
strokeWeight(5);
fill(255, 255, 255);
rect(250, 600, 150, 150);
stroke(0, 0, 0); // square
strokeWeight(5);
fill(255, 255, 255);
rect(650, 600, 150, 150);
stroke(0, 0, 0); // square
strokeWeight(5);
fill(255, 255, 255);
rect(1050, 600, 150, 150);
stroke(0, 0, 0); // square
strokeWeight(5);
fill(255, 255, 255);
rect(1450, 600, 150, 150);
//-----------------------------------------------------------
stroke(0, 0, 0); // square
strokeWeight(5);
fill(255, 255, 255);
rect(250, 850, 150, 150);
stroke(0, 0, 0); // square
strokeWeight(5);
fill(255, 255, 255);
rect(650, 850, 150, 150);
stroke(0, 0, 0); // square
strokeWeight(5);
fill(255, 255, 255);
rect(1050, 850, 150, 150);
stroke(0, 0, 0); // square
strokeWeight(5);
fill(255, 255, 255);
rect(1450, 850, 150, 150);
}
void keyPressed()
{
if ( key == 'r' ) Bongo.rewind();
if ( key == 'r' ) cello.rewind();
if ( key == 'r' ) drumset.rewind();
if ( key == 'r' ) flute.rewind();
if ( key == 'r' ) Guitar.rewind();
if ( key == 'r' ) handbells.rewind();
if ( key == 'r' ) harmonica.rewind();
if ( key == 'r' ) Harp.rewind();
if ( key == 'r' ) marimba.rewind();
if ( key == 'r' ) oboe.rewind();
if ( key == 'r' ) piano.rewind();
if ( key == 'r' ) saxphone.rewind();
if ( key == 'r' ) Trombone.rewind();
if ( key == 'r' ) trumpet.rewind();
if ( key == 'r' ) Tuba.rewind();
if ( key == 'r' ) violin.rewind();
if ( key == 'z' ) {
Bongo.play();
} else {
Bongo.pause();
}
if ( key == 'x' ) {
cello.play();
} else {
cello.pause();
}
if ( key == 'c' ) {
drumset.play();
} else {
drumset.pause();
}
if ( key == 'v' ) {
flute.play();
} else {
flute.pause();
}
if ( key == 'b' ) {
Guitar.play();
} else {
Guitar.pause();
}
if ( key == 'n' ) {
handbells.play();
} else {
handbells.pause();
}
if ( key == 'm' ) {
harmonica.play();
} else {
harmonica.pause();
}
if ( key == 'a' ) {
Harp.play();
} else {
Harp.pause();
}
if ( key == 's' ) {
marimba.play();
} else {
marimba.pause();
}
if ( key == 'd' ) {
oboe.play();
} else {
oboe.pause();
}
if ( key == 'f' ) {
piano.play();
} else {
piano.pause();
}
if ( key == 'g' ) {
saxphone.play();
} else {
saxphone.pause();
}
if ( key == 'h' ) {
Trombone.play();
} else {
Trombone.pause();
}
if ( key == 'j' ) {
trumpet.play();
} else {
trumpet.pause();
}
if ( key == 'k' ) {
Tuba.play();
} else {
Tuba.pause();
}
if ( key == 'l' ) {
violin.play();
} else {
violin.pause();
}
}
Answers
no need to keep repeating the if (key == 'r') bit, just put everything into one block
why are Bongo and Harp and Guitar capitalised? traditionally member names start with lower case, classes start with uppercase.
as for this:
does anything play? you seem to be pausing everything that isn't playing.
does fingerpos show up correctly?
oh, wait
greater than 350 and less than 150? is that what you mean?
also
there's no need to set stroke, strokeWeight, fill for every square - they will remain the same until you change them.
But now when i press the key will play music, also the leap motion move to the square will not play it
All your y checking is wrong, it doesn't match the squares. Have you fixed that?
Now i fix the code about y is work. Thanks a lot koogs