如果条件,则预期在else上表达

时间:2014-04-22 16:01:29

标签: ios if-statement

我有以下代码来确定游戏是否应该进入下一个级别。最后一个if和else条件导致和两个错误。 “期望的表达”和“预期的标识符。如果我评论这些条件,代码编译得很好。不确定我缺少什么。任何帮助将不胜感激。

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    for (UITouch *touch in touches) {
        SKNode *n = [self nodeAtPoint: [touch locationInNode:self]];
        if (n !=self && [n.name isEqual:@"restartLabel"] && _gameLevel == 0){
            [[self childNodeWithName:@"restartLabel"] removeFromParent];
            [[self childNodeWithName:@"winLoseLabel"] removeFromParent];
            SKTransition *reveal = [SKTransition flipHorizontalWithDuration:0.5];
            SKScene * myScene = [[MyScene alloc] initWithSize:self.size];
            [self.view presentScene:myScene transition: reveal];
            return;
        }
    else if (n !=self && [n.name isEqual:@"nextLevelLabel"] && _gameLevel == 1)
        {
            [[self childNodeWithName:@"nextLevelLabel"] removeFromParent];
            [[self childNodeWithName:@"winLoseLabel"] removeFromParent];
            SKTransition *reveal = [SKTransition flipHorizontalWithDuration:0.5];
            SKScene * myScene2 = [[MyScene2 alloc] initWithSize:self.size];
            [self.view presentScene:myScene2 transition: reveal];
            return;
        }
    }
    else if (n !=self && [n.name isEqual:@"nextLevelLabel"] && _gameLevel == 2)
        {
            [[self childNodeWithName:@"nextLevelLabel"] removeFromParent];
            [[self childNodeWithName:@"winLoseLabel"] removeFromParent];
            SKTransition *reveal = [SKTransition flipHorizontalWithDuration:0.5];
            SKScene * myScene3 = [[MyScene3 alloc] initWithSize:self.size];
            [self.view presentScene:myScene3 transition: reveal];
            return;
        }
}
    else
        {            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"End of the Road" message:@"You reached the end of the game. More levels coming soon!" delegate:self cancelButtonTitle:@"Ok"  nil]
            [alert show];
        }
    @end

2 个答案:

答案 0 :(得分:5)

第二个else if和最后一个else位于for循环的结束大括号之后。

我建议您使用大括号一致性和以下布局来避免这样的问题。

for (...) {
    if (...) {
    } else if (...) {
    } else if (...) {
    } else {
    }
}

这样做可以让它易于发现并且容易发现错误。

你可能更喜欢自己的花括号,这很好。选择一种风格并在每个地方使用它。您发布的代码使用两种不同的样式。这只会导致问题。

答案 1 :(得分:-1)

else if (n !=self && [n.name isEqual:@"nextLevelLabel"] && _gameLevel == 1)
    {
        [[self childNodeWithName:@"nextLevelLabel"] removeFromParent];
        [[self childNodeWithName:@"winLoseLabel"] removeFromParent];
        SKTransition *reveal = [SKTransition flipHorizontalWithDuration:0.5];
        SKScene * myScene2 = [[MyScene2 alloc] initWithSize:self.size];
        [self.view presentScene:myScene2 transition: reveal];
        return;
    }
}   <--- remove this