如何在触摸移动时从 CCLabelTTF 精灵获取唯一字母 cocos2d
How to get unique letter from CCLabelTTF sprite when touch moved in cocos2d
我有一个 8x8 字母矩阵游戏,当我触摸到标签时,标签值重复,如何解决这个问题,字母在 8x8 矩阵中的随机位置。
-(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]];
}
}
}
}
啊...单个网格元素中的 touchMoved 更新太多了!试试这个
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);
}
}
我有一个 8x8 字母矩阵游戏,当我触摸到标签时,标签值重复,如何解决这个问题,字母在 8x8 矩阵中的随机位置。
-(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]];
}
}
}
}
啊...单个网格元素中的 touchMoved 更新太多了!试试这个
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);
}
}