我有下面的代码,以确定游戏是否应该移动到下一个级别。最后一个如果和条件条件导致和两个错误。“预期表达式”和“预期标识符”。如果我将这些条件注释掉,代码就会编译得很好。不知道我遗漏了什么。如果能提供任何帮助,我们将不胜感激。
- (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发布于 2014-04-22 16:12:53
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发布于 2014-04-22 16:05:43
第二个else if和最后一个else是在for循环的闭锁大括号之后。
请允许我建议您使用卷曲大括号的一致性和下面的布局来避免这样的问题。
for (...) {
if (...) {
} else if (...) {
} else if (...) {
} else {
}
}这样做可以使其可读性强,容易发现错误。
你可能更喜欢在他们自己的线上的花括号,这很好。选择一种风格,然后随时随地使用它。您发布的代码使用了两种不同的样式。这只会导致问题。
https://stackoverflow.com/questions/23224561
复制相似问题