ahoura
ahoura

Reputation: 689

best way to create a timer for a game

I have a time limiter for a game I am doing, what I am doing right now is using an initial value and deducting one value per frame update. here is the code :

- (void) gameTime {
    fullTime = fullTime - 1;
    NSLog(@"%i", fullTime);
    float xScale = 10 * fullTime / 4000;
    //_game.timerGraphic.scaleX = 10;
    //[_game.timerGraphic setScaleX:10];
    [_game.timerGraphic setScaleX:xScale];    
}

now the problem is that timerGraphic is a CCSprite, and when I update the ScaleX it doesnt work unless its a absolute value, is there anyway i can make this smoother and resize the "timer" in a smoother way?

Upvotes: 0

Views: 175

Answers (1)

Sean
Sean

Reputation: 5820

The problem with this sort of approach is that is makes the timer in your app dependent on the frame rate. Cocos2D has some built in mechanisms for dealing with these sorts of issues. In particular, the idea is that each time you load a new frame, you update animation values based on the amount of time elapsed between the previous and current frame. Look at the "Making Things Move" section in this Cocos2D tutorial. It explains how to schedule a selector (i.e., create a timer to fire the -nextFrame: method). This method get passed a dt (delta time) argument that can be used for creating smooth animations.

Upvotes: 2

Related Questions