Verisutha,
How would I carry that example over to the ccTouchesEnded method? 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?
Any help would be greatly appreciated!
Here is how I am doing it now, but 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):
- (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)) {
touchIsValid = TRUE;
}
_playerDidAction = 0;
if (touchIsValid) {
validTouchCount++;
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;
}
}
}
}
}
- (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];
}
}
}