View Single Post
 
Old November 10th, 2013, 03:20 PM
thepianoguy thepianoguy is offline
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

Not sure why you think accessing the card or cardIndex will give you any useful information about the game being completed. These are transient values and are gone once the view is layed out. A couple of options exist for presenting an alertview at the end of the game. One would be to have either a static integer or an instance variable to keep track of the matches. When the count is equal to 32, you are done. Another would be, since the cards are the only subviews to the view controller's view, to present the alert when the subview count is equal to zero. Here is a demonstration of the latter approach, including a bug fix for the program as it is written.

Code:
        // A card has already been tapped, so test for a match
        if (firstTappedView.value == tappedCard.value)
        {
            NSLog(@"tappedCard is %@ and firstTappedView is %@",tappedCard,firstTappedView);
            // Player found a match
            // Remove the cards
            [UIView animateWithDuration:1 delay:1
                                options:UIViewAnimationOptionTransitionNone
                             animations:^{
                                 firstTappedView.alpha = 0; 
                                 tappedCard.alpha=0;
                             }
                             completion:^(BOOL finished) {
                                 
                                 [firstTappedView removeFromSuperview];
                                 [tappedCard removeFromSuperview];
                                 firstTappedView = nil;////RELOCATED FROM ORIGINAL CODE
                                 // Stop ignoring taps because animation is done
                                 isAnimating=NO;
                                 NSLog(@"subviews are %@",self.view.subviews);
                                 NSLog(@"the count is %d",self.view.subviews.count);
                                 
                                 if (self.view.subviews.count == 0) {////CALL TO ALERTVIEW
                                     [self performSelector:@selector(gameOver) withObject:nil afterDelay:.5];
                                 }
                             }
             ];
            
            
//            // Reset the first tapped card to nil
//            firstTappedView = nil; ////WRONG PLACE FOR THIS. THE VIEW IS GETTING SET TO NIL BEFORE THE COMPLETION BLOCK IS EXECUTED SO IT NEVER IS REMOVED FROM THE SUPERVIEW. I HAVE MOVED IT TO THE COMPLETION BLOCK TO CORRECT THIS
        }
        
        else
        {
            // Flip both cards back over
            secondTappedView = tappedCard;
            [self performSelector:@selector(flipCards) withObject:nil
                       afterDelay:2];
            
        }
    }
}

-(void)gameOver/////PRESENTS ALERTVIEW WITH FAIRLY OBVIOUS MESSAGE
{
    NSLog(@"GameOver!");
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Game Over!!" message:@"matches are completed" delegate:self cancelButtonTitle:@"Quit?" otherButtonTitles:@"Replay?", nil];
    [alert show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    ////IF REPLAY IS CHOSEN, THE PROGRAM STARTS OVER. OTHERWISE NOTHING.
    if (buttonIndex == 1) {
        [self createCards];
    }
    
}
Make sure the AlertView delegate is added to the header role.
Bob