匹配图像

时间:2012-05-04 06:33:38

标签: objective-c arrays cocoa

我有一个非常意外的问题让我发疯。这是一款非常简单的记忆游戏,有36张牌。在下面的代码中,我使用36个objets(18个双重对象)制作数组。当您选择1-12之间的卡时,一切都很好。当您选择更高的卡时,匹配过程无法识别匹配。为什么???

memory.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;
}

最终匹配过程

谢谢!

1 个答案:

答案 0 :(得分:0)

由于数组只能包含对象,因此不使用!=测试它们,因为它只会比较指针的值而不是对象本身。很多时候,这对某些(通常是低的)值有效(但通常很低),但是因为UIKit对象经常缓存对象,所以它会返回相同的对象地址,即使对于不同的实例也是如此。

您需要在tileClicked中更改此行:

if ([imagesArray objectAtIndex:clickedTileIndex] != [imagesArray objectAtIndex:openTileIndex]) {

到此:

if (![(UIImage *)[imagesArray objectAtIndex:clickedTileIndex] isEqual:
     (UIImage *)[imagesArray objectAtIndex:openTileIndex]]) {