我有以下代码来确定游戏是否应该进入下一个级别.最后一个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
Run Code Online (Sandbox Code Playgroud)
第二个else if和最后一个else是在for循环的闭合大括号之后.
我建议您使用大括号一致性和以下布局来避免这样的问题.
for (...) {
if (...) {
} else if (...) {
} else if (...) {
} else {
}
}
Run Code Online (Sandbox Code Playgroud)
这样做可以使其易读且容易发现错误.
你可能更喜欢自己的花括号,这很好.选择一种风格并在每个地方使用它.您发布的代码使用两种不同的样式.这只会导致问题.
| 归档时间: |
|
| 查看次数: |
2734 次 |
| 最近记录: |