Question

D'accord, donc, ce code est assez basique. L'utilisateur saisit une réponse dans une zone de texte, si elle est égale à « premier + seconde » ils obtiennent un point. Ensuite, ils ont 5 secondes pour répondre au prochain problème de mathématiques. Si elles le font, la fonction « doCalculation » est exécuté à nouveau, et ils obtiennent un autre point. Dans le cas contraire, la fonction « ontimer » est exécuté, et la merde frappe le ventilateur.

Le problème est, lorsque l'utilisateur obtient de multiples problèmes dans une ligne correcte, « doCalculation » est exécuté plusieurs fois, puis-je avoir plusieurs minuteries qui vont à la fois. Cela commence vraiment vissant le jeu.

Je dois arrêter les minuteries. De toute évidence utiliser « invalident » mais je ne sais pas où. Je ne peux pas annuler la minuterie avant qu'il ne commence, alors ... whhhhatt?

Une autre option que je ne suis pas sûr de savoir comment faire, si le lorsque vous recevez le problème corriger suffit simplement de définir la minuterie de retour à 5 secondes au lieu de créer un nouveau. Mais comment puis-je savoir si la minuterie a déjà été créé? Je ne suis pas sûr de ce que le meilleur plan d'action est, ou la syntaxe. Pensées?

Merci beaucoup!

- (IBAction)doCalculation:(id)sender
{
    NSInteger numAnswer = [answer.text intValue];
    if ( numAnswer == first + second) {
        numAnswered++;
        NSString *numberAnsweredCorrectly = [[NSString alloc] initWithFormat:@"%d", numAnswered];
        numCorrectlyAnswered.text = numberAnsweredCorrectly;
        answer.text = @"";

        NSTimer *mathTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];     

        //Set the variables to two HUGE numbers, so they can't keep plugging in the same answer

        first = arc4random() % 10;
        second = arc4random() % 10;

        NSString *firstString = [[NSString alloc] initWithFormat:@"%d", first];
        NSString *secondString = [[NSString alloc] initWithFormat:@"%d", second];

        firstNumber.text = firstString;
        secondNumber.text = secondString;
    }
Était-ce utile?

La solution

Je propose la mathTimer dans votre tête de classe:

//inside your .f file:
@interface YourClassNAme : YourSuperClassesName {
    NSTimer *mathTimer
}


@property (nonatomic, retain) NSTimer *mathTimer;

//inside your .m:
@implementation YourClassNAme 
@synthesize mathTimer;

-(void) dealloc {
   //Nil out [release] the property
   self.mathTimer = nil;
   [super dealloc];
}

Et changer votre méthode pour accéder à la propriété par minuterie:

- (IBAction)doCalculation:(id)sender
{
    NSInteger numAnswer = [answer.text intValue];
    if ( numAnswer == first + second) {
        numAnswered++;
        NSString *numberAnsweredCorrectly = [[NSString alloc] initWithFormat:@"%d", numAnswered];
        numCorrectlyAnswered.text = numberAnsweredCorrectly;
        answer.text     = @"";

        [self.mathTimer invalidate];  //invalidate the old timer if it exists
        self.mathTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];             

        //Set the variables to two HUGE numbers, so they can't keep plugging in the same answer

        first = arc4random() % 10;
        second = arc4random() % 10;

        NSString *firstString = [[NSString alloc] initWithFormat:@"%d", first];
        NSString *secondString = [[NSString alloc] initWithFormat:@"%d", second];

        firstNumber.text = firstString;
        secondNumber.text = secondString;
    }

Autres conseils

Si vous vous faites un jeu, vous devez utiliser une boucle de jeu où le jeu est mis à jour dans cette boucle. Ensuite, vous pouvez vérifier les résultats après le temps est écoulé. Vous n'aurez 1 minuterie pour traiter continuos.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top