Search code examples
iosobjective-csprite-kitbooleanskscene

BOOL gives different result in if statement at update:(CFTimeInterval)currentTime method


I have two scenes - DifficultScene and GameScene. In DifficultScene I have three buttons - easy, medium and hard. I use a global variable Bool to keep track of the current difficulty level. When I try easy mode everything works fine, but when I try medium or hard, bool is changing every second, jumping from hard to medium and easy, making game unplayable. My question is - how can I fix it? Here are code were it happens:
GamesScene.m

-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
extern BOOL isEasyMode;
extern BOOL isMediumMode;
extern BOOL isHardMode;
if ((isEasyMode = YES)) {
    NSLog(@"easy");
    [self computer];
}
if ((isMediumMode = YES)) {
    NSLog(@"medium");
    [self computerMedium];
}
if ((isHardMode = YES)) {
    NSLog(@"hard");
    [self computerHard];
}

[self scoreCount];
}

(if more code is needed, i will post it)


Solution

  • I think your update method calls periodically as per timer so it will get called continuously if it so then. Thats why it is happening i think and another major thing is you should use == for comparison. you are using (isEasyMode = YES) that means you are assigning YES to isEasyMode.

    So replce all if statement like if ((isEasyMode = YES)) with if (isEasyMode == YES).

    Update :

    if statement should like,

      if (isEasyMode == YES) {
        NSLog(@"easy");
        [self computer];
    }
    

    Hope this will help :)