I would like to know how to detect and differentiate between touches in a multi-touch view. I have read about a "hash" code, but I don't understand how to use it. I want to know when two of my Sprites are touched at the same time, like as if pressing a chord on two keys of a piano.
[EDIT] Here is an example of what I have for my ccTouchesBegan:
- (void) ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
NSSet *allTouches = [event allTouches];
int validTouchCount = 0;
for (UITouch* touch in allTouches) {
BOOL touchIsValid = FALSE;
CGPoint location = [touch locationInView: [touch view]];
CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];
if (CGRectContainsPoint(_fourButtonsRect, convertedLocation)) {
NSLog(@"Touch is within four buttons");
touchIsValid = TRUE;
}
_playerDidAction = 0;
NSLog(@"before the loop");
if (touchIsValid) {
validTouchCount++;
NSLog(@"Within ValidTouches loop");
CGPoint validLocation = [touch locationInView: [touch view]];
CGPoint convertedValidLocation = [[CCDirector sharedDirector] convertToGL:validLocation];
if (CGRectContainsPoint(_redButtonSprite.boundingBox, convertedValidLocation)) {
_redButtonStatus = TRUE;
[_redButtonSprite setTexture:_redButtonLit];
if (validTouchCount == 1) {
_playerDidAction = 1;
}
}
else if (CGRectContainsPoint(_blueButtonSprite.boundingBox, convertedValidLocation)) {
_blueButtonStatus = TRUE;
[_blueButtonSprite setTexture:_blueButtonLit];
if (validTouchCount == 1) {
_playerDidAction = 2;
}
}
else if (CGRectContainsPoint(_greenButtonSprite.boundingBox, convertedValidLocation)) {
_greenButtonStatus = TRUE;
[_greenButtonSprite setTexture:_greenButtonLit];
if (validTouchCount == 1) {
_playerDidAction = 3;
}
}
else if (CGRectContainsPoint(_yellowButtonSprite.boundingBox, convertedValidLocation)) {
_yellowButtonStatus = TRUE;
[_yellowButtonSprite setTexture:_yellowButtonLit];
if (validTouchCount == 1) {
_playerDidAction = 4;
}
}
if (validTouchCount > 1) {
if (_redButtonStatus && _blueButtonStatus) {
_comboRB = TRUE;
_playerDidAction = 5;
}
else if (_redButtonStatus && _greenButtonStatus) {
_comboRG = TRUE;
_playerDidAction = 6;
}
else if (_redButtonStatus && _yellowButtonStatus) {
_comboRY = TRUE;
_playerDidAction = 7;
}
else if (_blueButtonStatus && _greenButtonStatus) {
_comboBG = TRUE;
_playerDidAction = 8;
}
else if (_blueButtonStatus && _yellowButtonStatus) {
_comboBY = TRUE;
_playerDidAction = 9;
}
else if (_greenButtonStatus && _yellowButtonStatus) {
_comboGY = TRUE;
_playerDidAction = 10;
}
}
}
}
}
And here is the beginning of my ccTouchesEnded:
- (void)ccTouchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
for (UITouch *touch in touches) {
CGPoint location = [touch locationInView: [touch view]];
CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];
if (CGRectContainsPoint(_redButtonSprite.boundingBox, convertedLocation)) {
_redButtonStatus = FALSE;
[_redButtonSprite setTexture:_redButtonNormal];
}
if (CGRectContainsPoint(_blueButtonSprite.boundingBox, convertedLocation)) {
_blueButtonStatus = FALSE;
[_blueButtonSprite setTexture:_blueButtonNormal];
}
if (CGRectContainsPoint(_greenButtonSprite.boundingBox, convertedLocation)) {
_greenButtonStatus = FALSE;
[_greenButtonSprite setTexture:_greenButtonNormal];
}
if (CGRectContainsPoint(_yellowButtonSprite.boundingBox, convertedLocation)) {
_yellowButtonStatus = FALSE;
[_yellowButtonSprite setTexture:_yellowButtonNormal];
}
}
}
Could you maybe give me an example of how you would capture touches that began on a sprite and ended on a sprite? I have been struggling and can't get the hash code to work - just not understanding how the hash code can be used to reference a touch later. I guess what I'm trying to make would be called a hash tracker?
I am sure there is a much less convoluted way to do it using the hash codes and less state variables. I haven't fleshed out the ccTouchesEnded method with the other state variable effects because I was hoping to find a simpler way (I know I still need to make the ccTouchesMoved and Canceled methods too).