5
\$\begingroup\$

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?

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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