Search code examples
cocos2d-iphone

How to keep fps rate constant in cocos2d


I have 3 question.

  1. How to keep fps rate constant(almost) in cocos2d.When lots of CCSprite created and removed within a small interval like (5-8) per second is it possible to keep frame rate lmost constant??

  2. Is [self removeChild:sprite cleanup:YES] is enough or I should also use

      CCTexture2D *texture = [sprite texture]; [[CCTextureCache sharedTextureCache] removeTexture: texture];
    
  3. The following Part of code is responsible for my frame drop.How to accomplish same task in better way?

    id fadeout = [CCFadeOut actionWithDuration:1.4f];
    
    id call = [CCCallFunc actionWithTarget:self   
    selector:@selector(RemoveSmashedSprite:)];
    
    CCSequence* sequence= [CCSequence
    actions:fadeout, call, nil];
    
    [smash runAction:sequence];
    

    ... and...

    > -(void)RemoveSmashedSprite:(id)sender
    {
    
    
        CCSprite *sp = (CCSprite *)sender;
    
    
    
        [self removeChild:sp cleanup:YES];  
    
    }
    

This is called 5-8 times per second. So the frame rate goes down. Can any one help me?


Solution

  • You shouldn't remove textures if you're going to reuse them in the short term. It would only benefit memory while having a great drawback in performance.

    To maintain a constant frame rate you could try reusing the sprites instead of creating and removing them. Instead of calling removeChild you could set visible = NO and add it to an array of unused sprites. Then when you need a new of these sprites you check if there is any in that unused array and only create a new one if it's empty. This way you would minimize the amount of sprites created and destroyed.