Note that there are some explanatory texts on larger screens.

plurals
  1. POIn cocos2d how do you implement a touch hash code and reference it later?
    text
    copied!<p>How do you implement a touch hash code and reference it later? 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.</p> <p>Here is an example of what I have for my ccTouchesBegan:</p> <pre><code>- (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 &gt; 1) { if (_redButtonStatus &amp;&amp; _blueButtonStatus) { _comboRB = TRUE; _playerDidAction = 5; } else if (_redButtonStatus &amp;&amp; _greenButtonStatus) { _comboRG = TRUE; _playerDidAction = 6; } else if (_redButtonStatus &amp;&amp; _yellowButtonStatus) { _comboRY = TRUE; _playerDidAction = 7; } else if (_blueButtonStatus &amp;&amp; _greenButtonStatus) { _comboBG = TRUE; _playerDidAction = 8; } else if (_blueButtonStatus &amp;&amp; _yellowButtonStatus) { _comboBY = TRUE; _playerDidAction = 9; } else if (_greenButtonStatus &amp;&amp; _yellowButtonStatus) { _comboGY = TRUE; _playerDidAction = 10; } } } } } </code></pre> <p>And here is the beginning of my ccTouchesEnded:</p> <pre><code>- (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]; } } } </code></pre> <p>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?</p> <p>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).</p> <p>Any advise would be very appreciated!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload