I've had a great deal of trouble getting multitouch to work properly in touchesBegan. I've found plenty of tuts online but none have worked for me so far. I'm trying to make a guitar type app. I can get touchesMoved to fire properly but touching down on the notes only recognizes the first touch. What have I missed?
Thanks,
Nick
Code:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//NSSet *touchy = [touches setByAddingObjectsFromSet:[event allTouches]];
//NSLog(@"Adding %d touches.", [[touchy allObjects] count]);
//NSUInteger *touch = [[touches anyObject] tapCount];
////for (UITouch *touch in touches){
//Touch *touch = [touches anyObject];
//[self setMultipleTouchEnabled:YES];
//NSSet *touches = [[event allTouches] anyObject];
//NSMutableSet *currentTouches = [[[event touchesForView:self]mutableCopy] autorelease];
//[currentTouches minusSet:touches];
//int nb = [touch count];
UITouch *touch = [[event allTouches] anyObject];
CGPoint location1 = [touch locationInView:touch.view];
CGPoint location1b = [touch locationInView:touch.view];
CGPoint location1c = [touch locationInView:touch.view];
CGPoint location1d = [touch locationInView:touch.view];
//if ([touches count] == 1)
//for (UITouch *touch in touches){
//if ([currentTouches count] >0) {
NSLog(@"1");
if(CGRectContainsPoint(pianoButton1.frame, location1))
{ if (label1.text == @"0")
{[pianoButton1 setHighlighted:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"uku G" ofType:@"wav"];
AVAudioPlayer * myMusic11=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.myMusic = myMusic11;
myMusic.delegate = self;
[myMusic play];}
else if (label1.text == @"1"){[pianoButton1 setHighlighted:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"uku G#" ofType:@"wav"];
AVAudioPlayer * myMusic11=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.myMusic = myMusic11;
myMusic.delegate = self;
[myMusic play];}
if (label1.text == @"2")
{[pianoButton1 setHighlighted:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"uku A" ofType:@"wav"];
AVAudioPlayer * myMusic11=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.myMusic = myMusic11;
myMusic.delegate = self;
[myMusic play];}
else if (label1.text == @"3"){[pianoButton1 setHighlighted:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"uku A#" ofType:@"wav"];
AVAudioPlayer * myMusic11=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.myMusic = myMusic11;
myMusic.delegate = self;
[myMusic play];}
if (label1.text == @"4")
{[pianoButton1 setHighlighted:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"uku B" ofType:@"wav"];
AVAudioPlayer * myMusic11=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.myMusic = myMusic11;
myMusic.delegate = self;
[myMusic play];}
else if (label1.text == @"5"){[pianoButton1 setHighlighted:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"uku 2C" ofType:@"wav"];
AVAudioPlayer * myMusic11=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.myMusic = myMusic11;
myMusic.delegate = self;
[myMusic play];}
}else {
[pianoButton1 setHighlighted:NO];
}
if(CGRectContainsPoint(pianoButton2.frame, location1b))
{ if (label2.text == @"0")
{[pianoButton2 setHighlighted:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"uku C" ofType:@"wav"];
AVAudioPlayer * myMusic22=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.myMusic2 = myMusic22;
myMusic2.delegate = self;
[myMusic2 play];
}
if (label2.text == @"1")
{[pianoButton2 setHighlighted:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"uku C#" ofType:@"wav"];
AVAudioPlayer * myMusic22=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.myMusic2 = myMusic22;
myMusic2.delegate = self;
[myMusic2 play];
}
if (label2.text == @"2")
{[pianoButton2 setHighlighted:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"uku D" ofType:@"wav"];
AVAudioPlayer * myMusic22=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.myMusic2 = myMusic22;
myMusic2.delegate = self;
[myMusic2 play];
}
if (label2.text == @"3")
{[pianoButton2 setHighlighted:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"uku D#" ofType:@"wav"];
AVAudioPlayer * myMusic22=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.myMusic2 = myMusic22;
myMusic2.delegate = self;
[myMusic2 play];
}
if (label2.text == @"4")
{[pianoButton2 setHighlighted:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"uku E" ofType:@"wav"];
AVAudioPlayer * myMusic22=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.myMusic2 = myMusic22;
myMusic2.delegate = self;
[myMusic2 play];
}
if (label2.text == @"5")
{[pianoButton2 setHighlighted:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"uku F" ofType:@"wav"];
AVAudioPlayer * myMusic22=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.myMusic2 = myMusic22;
myMusic2.delegate = self;
[myMusic2 play];
}
}else {
[pianoButton2 setHighlighted:NO];
}
if(CGRectContainsPoint(pianoButton3.frame, location1c))
{ if (label3.text == @"0")
{[pianoButton3 setHighlighted:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"uku E" ofType:@"wav"];
AVAudioPlayer * myMusic33=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.myMusic3 = myMusic33;
myMusic3.delegate = self;
[myMusic3 play];}
else if (label3.text == @"1"){[pianoButton3 setHighlighted:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"uku F" ofType:@"wav"];
AVAudioPlayer * myMusic33=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.myMusic3 = myMusic33;
myMusic3.delegate = self;
[myMusic3 play];}
if (label3.text == @"2")
{[pianoButton3 setHighlighted:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"uku F#" ofType:@"wav"];
AVAudioPlayer * myMusic33=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.myMusic3 = myMusic33;
myMusic3.delegate = self;
[myMusic3 play];}
else if (label3.text == @"3"){[pianoButton3 setHighlighted:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"uku G" ofType:@"wav"];
AVAudioPlayer * myMusic33=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.myMusic3 = myMusic33;
myMusic3.delegate = self;
[myMusic3 play];}
if (label3.text == @"4")
{[pianoButton3 setHighlighted:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"uku G#" ofType:@"wav"];
AVAudioPlayer * myMusic33=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.myMusic3 = myMusic33;
myMusic3.delegate = self;
[myMusic3 play];}
else if (label3.text == @"5"){[pianoButton3 setHighlighted:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"uku A" ofType:@"wav"];
AVAudioPlayer * myMusic33=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.myMusic3 = myMusic33;
myMusic3.delegate = self;
[myMusic3 play];}
}else {
[pianoButton3 setHighlighted:NO];
}
if(CGRectContainsPoint(pianoButton4.frame, location1d))
{ if (label4.text == @"0")
{[pianoButton4 setHighlighted:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"uku A" ofType:@"wav"];
AVAudioPlayer * myMusic44=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.myMusic4 = myMusic44;
myMusic4.delegate = self;
[myMusic4 play];}
else if (label4.text == @"1"){[pianoButton4 setHighlighted:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"uku A#" ofType:@"wav"];
AVAudioPlayer * myMusic44=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.myMusic4 = myMusic44;
myMusic4.delegate = self;
[myMusic4 play];}
if (label4.text == @"2")
{[pianoButton4 setHighlighted:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"uku B" ofType:@"wav"];
AVAudioPlayer * myMusic44=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.myMusic4 = myMusic44;
myMusic4.delegate = self;
[myMusic4 play];}
else if (label4.text == @"3"){[pianoButton4 setHighlighted:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"uku 2C" ofType:@"wav"];
AVAudioPlayer * myMusic44=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.myMusic4 = myMusic44;
myMusic4.delegate = self;
[myMusic4 play];}
if (label4.text == @"4")
{[pianoButton4 setHighlighted:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"uku 2C#" ofType:@"wav"];
AVAudioPlayer * myMusic44=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.myMusic4 = myMusic44;
myMusic4.delegate = self;
[myMusic4 play];}
else if (label4.text == @"5"){[pianoButton4 setHighlighted:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"uku 2D" ofType:@"wav"];
AVAudioPlayer * myMusic44=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.myMusic4 = myMusic44;
myMusic4.delegate = self;
[myMusic4 play];}
}else {
[pianoButton4 setHighlighted:NO];
}
}
Thanks,
Nick