Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am building a simple tower defense game, and I have to take a decision between two(or more, feel free to suggest other ways) to do the game cycle.

Option 1: Monolithic game cycle:

I could have a global scheduled selector that calls update for every sprite/object/logic thing that I need to calculate.

I could achieve this by having a singelton Game class that has a mutable array of objects and then on the Scene(or that singelton) I could have a setup with:

[self schedule: @selector(gameCycle:) interval:0.1];

And then on that gameCycle do something like:

-(void) gameCicle: (ccTime)dt {
    for(Entity* e in [[Game sharedGame] entities]){
        [e gameCicle];
    }
}

Second option: Each object could create its own selector @ init. This is almost the same, but instead of a singleton, every time that i create a object I could create a new scheduled selector as such:

-(id) initWithTexture:(CCTexture2D*)texture rect:(CGRect)rect {
    if( (self=[super initWithTexture:texture rect:rect])) {
        [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
            [self schedule: @selector(gameCycle:) interval:0.1];
    }
    return self;
}

Now I don't know what is better(or if there is a very big difference in performance). What is the usual/best approach for cocos2d?

share|improve this question
add comment

2 Answers

Use the first one. You certainly don't want your timers be called in random order.

Although I'd suggest to actually use none of them, but use the standard update method of cocos2d. This will give you the best available performance and consistent behavior.

share|improve this answer
    
I don't see any documentation on that in the best practices (code.google.com/p/cocos2d-iphone/wiki/BestPractices) could you share me some links/or an example. BTW I am NOT drawing from this scheduled selectors. –  DFectuoso Aug 20 '10 at 17:07
1  
Actually according to cocos2d-iphone.org/wiki/doku.php/prog_guide:draw_update "Each node, by default, has no updates. If you want to update the state of your node you should schedule a callback". –  DFectuoso Aug 20 '10 at 17:59
add comment

There is a third option: have a small number of scheduled updates for tasks that can be independent.

Scheduling one update method for each layer could be useful, especially if they are logically separate. One useful example of this is using one scheduled method for the main game logic and another to update the score display.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.