开发者

Expected ')' before ';' token error?

开发者 https://www.devze.com 2023-02-19 13:46 出处:网络
I am creating an Iphone app, and It works by an NSTimer that calls the -(void)gameLoop every sixtieth of a second. here is the gameLoop

I am creating an Iphone app, and It works by an NSTimer that calls the -(void)gameLoop every sixtieth of a second. here is the 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];

}



}

I get the error "Expected ')' before ';' token" in every if statement and in the ParatrooperTimer+=1 line.

GameState is and Integer, and so are all of the KGameState... . Please Help me! Thanks a bunch


This isn't what your question is about, but you've raised a red flag for me that I've run into before and you would probably appreciate some advance warning about.

NSTimer fires at the end of the event loop. It's not a metronome--it gets called when it gets called, and it might not be regular at all. A long process that blocks the app will prevent an NSTimer from getting fired on time. Also an NSTimer has a max resolution of 50-100ms (per the docs). So in the BEST case, it'll fire 20 times a second, and you're trying to ask it for three times finer resolution than that.

For lower-resolution stuff, NSTimer is great, but to pulse as fast as you want it to, it probably doesn't work at all. But then, do you really need 60 frames/sec?


You probably have an unbalanced parenthesis somewhere above - (void)gameLoop, or maybe you left out the semicolon from method declaration - (void)gameLoop; in your .h file (but I think that would give you a different error message).


You have to add (NSTimer *) aTimer to your function signature like this:

-(void)gameLoop :(NSTimer *) aTimer

and in the selector of [NSTimer scheduleWithTimeInterval... you should add a semicolon:

@selector(gameLoop:)

As for your function:

what is this line about in your last if statement and why do you need parenthesis there?

(paratrooperTimer = 0);

Probably this is the cause of the compiler error


I'd suspect either parenthesis typo or invalid timer signature.

Example for timer:

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

where timerHandler is defined as:

- (void)updateTimeView:(id)inTimer;  

Anyway, why don't you use switch-case statement? With lots of if it can help maintain readbility of the code, so parenthesis typos can be found easier and faster. Switch-case explanation

0

精彩评论

暂无评论...
验证码 换一张
取 消