Search code examples
ioscocos2d-iphoneccsprite

How to get unique letter from CCLabelTTF sprite when touch moved in cocos2d


I have a 8x8 matrix of alphabet letters game,when I move touch into label,label value repeated,how to overcome this problem,letters are in random position in 8x8 matrix.

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
   UITouch *touch=[touches anyObject];
   CGPoint newlocation=[touch locationInView:[touch view]];

   newlocation=[[CCDirector sharedDirector] convertToGL:newlocation];
   for (int i=0; i<GRID_WIDTH; i++) {
      for (int j=0; j<GRID_HEIGHT; j++) {
         if (CGRectContainsPoint([grid[i][j].letter boundingBox],newlocation)) {
            CCLOG(@"letters %@",[grid[i][j].letter string]);
            [letterarray addObject:[grid[i][j].letter string]];
         }
      }
   }
}

Solution

  • ah ... too many touchMoved updates in a single grid element ! try this

         if (CGRectContainsPoint([grid[i][j].letter boundingBox],newlocation)) {
            CCLOG(@"letters %@",[grid[i][j].letter string]);
            NSString *thisLetter = [grid[i][j].letter string];
            if (![letterArray containsObject:thisLetter]) {
                [letterarray addObject:thisLetter];
            } else {
                NSLog(@"*** Skipping letter, [%@] already in the array",thisLetter);
            }
         }