2011-03-25 143 views
1

我正在创建一个Iphone应用程序,它由NSTimer每六十分之一秒调用一次 - (void)gameLoop。这里是gameLoop预期的')'在'之前';'令牌错误?

-(void)gameLoop { 

paratrooperTimer += 1; 

if (gameState == KGameStateBegin) { 
    BtnStart.hidden = 0; 
    BtnResume.hidden = 1; 
    BtnPause.hidden = 1; 
} 
else if (gameState == KGameStateRunning) { 

    BtnStart.hidden = 1; 
    BtnPause.hidden = 0; 

    [self playGameLoop]; 
} 
else if (gameState == KGameStatePaused) { 

    BtnResume.hidden = 0; 
    BtnPause.hidden = 1; 
} 
else if (gameState == KGameStateGameOver) { 

    [self endGame]; 
} 
else if (paratrooperTimer == 120) { 

    (paratrooperTimer = 0); 
    [self spawnParatrooper]; 

} 



} 

我得到的错误“之前预计 ')' ';'令牌“在每个if语句和ParatrooperTimer + = 1行中。

GameState是和Integer,所有的KGameState也是如此。 请帮帮我! 多谢

+0

您在这里提交的代码有不平衡的花括号。无论是因为你的代码不平衡还是因为部分复制粘贴,我们都无法辨别。当然,你在这里提出的是一个语法错误。 – 2011-03-25 14:42:29

回答

1

你可能有一个不平衡的括号某处以上- (void)gameLoop,也许你在.h文件中遗漏了从方法声明- (void)gameLoop;分号(但我认为这会给你一个不同的错误信息)。

0

你得(的NSTimer *)aTimer添加到您的函数签名是这样的:

-(void)gameLoop :(NSTimer *) aTimer 

,并在[NSTimer scheduleWithTimeInterval...选择你要添加一个分号:

@selector(gameLoop:) 

至于你的功能:

你最后一条if语句中的这条线是什么?为什么你需要括号?

(paratrooperTimer = 0); 

也许这是编译器错误

2

这不是你的问题是什么的原因,但你提的问题红旗我,我以前碰到,你会可能会欣赏一些预先警告。

NSTimer在事件循环结束时触发。它不是一个节拍器 - 当它被调用时它会被调用,而且它可能不是常规的。阻止应用程序的漫长过程将会阻止NSTimer按时被触发。另外NSTimer的最大分辨率为50-100ms(每个文档)。所以在最好的情况下,它会每秒发射20次,而你试图要求它的分辨率高出三倍。

对于分辨率较低的东西,NSTimer非常好,但为了尽可能快地达到您想要的效果,它可能根本不起作用。但是,你真的需要60帧/秒吗?

0

我怀疑括号错误或无效的计时器签名。

举例定时器:

self.timer = [NSTimer scheduledTimerWithTimeInterval:60.0 
               target:self 
              selector:@selector(timerHandler:) 
              userInfo:nil 
              repeats:YES]; 

其中timerHandler被定义为:

- (void)updateTimeView:(id)inTimer; 

无论如何,你为什么不使用switch-case声明?有很多if它可以帮助保持代码的可读性,因此可以更容易和更快地找到括号拼写错误。 Switch-case explanation

相关问题