我有一个非常出乎意料的问题,这让我抓狂。这是一种非常简单的记忆游戏,有 36 张卡片。在下面的代码中,我用 36 个对象(18 个双对象)制作数组。当您在 1-12 之间选择卡片时,一切都很好。当您选择更高的卡时,匹配过程无法识别匹配。为什么???
内存.m
for (int i = 0; i < [imagesArray count]; ++i)
{
int n = i;
[imagesArray exchangeObjectAtIndex:i withObjectAtIndex:n];
NSLog(@"index obrázku %i",i);
NSLog(@"index array %i",n);
}
// Set button images
for (int i=0; i<[buttonsArray count]; i++) {
NSLog(@"index button %i",i);
UIButton *singleButton = [buttonsArray objectAtIndex:i];
[singleButton setBackgroundImage:[UIImage imageNamed:TILE_BACKGROUND] forState:UIControlStateNormal];
[singleButton setBackgroundImage:[UIImage imageNamed:TILE_BACKGROUND] forState:UIControlStateHighlighted];
if ([[imagesArray objectAtIndex:i] integerValue] == 1) {
[singleButton setBackgroundImage:[UIImage imageNamed:TILE_IMAGE1] forState:UIControlStateDisabled];
NSLog(@"Karta 1 tag %i",i);
} else if ([[imagesArray objectAtIndex:i] integerValue] == 2) {
[singleButton setBackgroundImage:[UIImage imageNamed:TILE_IMAGE2] forState:UIControlStateDisabled];
NSLog(@"Karta 2 tag %i",i);
}
对象是从 1 到 18...
- (IBAction)tileClicked:(id)sender {
// Button is clicked
if (!matchingInPogress) {
UIButton *clickedButton = sender;
tilesOpened++;
NSLog(@"Počet otočených karet je %i",tilesOpened);
[clickedButton setEnabled:NO];
if (tilesOpened == 1) {
// Only one tiled opened, remeber tag for match
openTileIndex = clickedButton.tag;
} else {
// Second tile is opened, perform match
matchingInPogress = YES;
[self performSelector:@selector(matchTiles:) withObject:[NSNumber numberWithInt:clickedButton.tag] afterDelay:0.5];
tilesOpened = 0;
}
}
按钮控制..
- (void)matchTiles:(NSNumber *)clickedTile {
// Match opened tiles
int clickedTileIndex = [clickedTile integerValue];
NSLog(@"Karta 1 tag %i",clickedTileIndex);
NSLog(@"Karta 2 tag %i",openTileIndex);
if ([imagesArray objectAtIndex:clickedTileIndex] != [imagesArray objectAtIndex:openTileIndex]) {
NSLog(@"Porovnává hodnoty v clickedTileIndex a openTileIndex");
// If tiles don't match set them to enabled state
UIButton *firstButton = [buttonsArray objectAtIndex:openTileIndex];
UIButton *secondButton = [buttonsArray objectAtIndex:clickedTileIndex];
NSLog(@"Nesouhlas tag je %i",openTileIndex);
NSLog(@"Nesouhlas tag je %i",clickedTileIndex);
[firstButton setEnabled:YES];
[secondButton setEnabled:YES];
} else {
NSLog(@"Match!");
UIButton *firstButton = [buttonsArray objectAtIndex:openTileIndex];
UIButton *secondButton = [buttonsArray objectAtIndex:clickedTileIndex];
NSLog(@"Souhlas tag je %i",openTileIndex);
NSLog(@"Souhlas tag je %i",clickedTileIndex);
[firstButton setHidden:YES];
[secondButton setHidden:YES];
}
matchingInPogress = NO;
}
最后匹配过程
谢谢!