2012-05-04 70 views
-2

我有一個非常意想不到的問題,讓我發瘋。這是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; 
} 

和finaly匹配處理

由於!

+1

這是什麼,Objective-C的? – JJJ

+0

@Juhana:我猜Obj-C/Cocoa ...我正在設置標籤 – Seki

+0

這裏有一個明顯缺乏信息imo –

回答

0

由於數組只能包含對象,因此您不會使用!=進行測試,因爲這隻會比較指針的值而不是對象本身的值。很多時候,對於某些(通常較低的)值,這種方式很有效(即使您不期望它),但因爲UIKit對象經常緩存對象而返回相同的對象地址,即使對於不同的實例也是如此。

您需要更改這一行tileClicked:

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

這樣:

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