Redrawing GameHUD multiple times causes cocos2d frame rates to drop significantly?

Try not to create and remove sprites at runtime, ie try to avoid doing this frequently: CCSprite spriteWithFile:@"background. Png" This allocates new memory for the sprite, and theres quite a bit of things going on behind the scenes when you create a new sprite. And of course you're releasing the already existing sprites.

All of that is unnecessary In your redrawGameHUD method I see no indication why you actually want to create the sprites anew. The sprites are using the same images every time. So why not just keep the old ones?

Unless you edited the code before you posted it in the questions, there's no need to remove and re-create the HUD sprites You also might want to create a texture atlas for all the HUD sprite images. For one, you can then render all HUD sprites with one draw call by using a CCSpriteBatchNode. Secondly, whenever you do want to assign a new texture to an existing sprite, you would simply change the CCSpriteFrame of that sprite instead of throwing away the sprite and re-creating it Something else that bothers me, you keep writing this: GameHUD class sharedHUD addChild:sprite First, this is the same as writing the following, the message to class is absolutely unnecessary (makes me wonder where you picked that up?): GameHUD sharedHUD addChild:sprite And since you do this multiple times over, you should keep a temporary local copy of GameHUD, this again removes several unnecessary Objective-C messages: GameHUD* gameHUD = GameHUD sharedHUD; // from now on use gameHUD instead of GameHUD sharedHUD gameHUD addChild:sprite This is particularly good advice for loops, because doing this: for (CCSprite *entity in GameHUD class sharedHUD buildable) will send two extra messages (class and sharedHUD) for every entity in the buildable array.

Those extra calls can quickly add up, although they're certainly not enough for the drop in framerate you're experiencing You also unnecessarily keep all the HUD sprites in the "buildable" array. Why not use the already existing children array that cocos2d uses? Simply add each HUD sprite that is "buildable" with the same tag, for example 123 gameHUD addChild:sprite z:0 tag:123 If you need to do something with all the "buildable" sprites you can then iterate over the children like this: CCNode* node; CCARRAY_FOREACH(gameHUD children, node) { if (node.

Tag == 123) { CCSprite* buildable = (CCSprite*)node; // do stuff with buildable sprite ... } } Again, this avoids the unnecessary adding, retaining, removing and releasing of objects in the buildable array. And you can be sure that you don't accidentally remove sprites from the node hierarchy but not the buildable array, or vice versa I'd like to conclude with an assumption: in your code I saw a general tendency that you're doing many extra things unnecessarily.So I'm guessing this is the case throughout the project. You might want to go back and ask yourself (better: find out) what other things there are that you're having the device perform that are rather unnecessary.

Try not to create and remove sprites at runtime, ie try to avoid doing this frequently: CCSprite spriteWithFile:@"background. Png"; This allocates new memory for the sprite, and theres quite a bit of things going on behind the scenes when you create a new sprite. And of course you're releasing the already existing sprites.

All of that is unnecessary. In your redrawGameHUD method I see no indication why you actually want to create the sprites anew. The sprites are using the same images every time.So why not just keep the old ones?

Unless you edited the code before you posted it in the questions, there's no need to remove and re-create the HUD sprites. You also might want to create a texture atlas for all the HUD sprite images. For one, you can then render all HUD sprites with one draw call by using a CCSpriteBatchNode.

Secondly, whenever you do want to assign a new texture to an existing sprite, you would simply change the CCSpriteFrame of that sprite instead of throwing away the sprite and re-creating it. Something else that bothers me, you keep writing this: GameHUD class sharedHUD addChild:sprite; First, this is the same as writing the following, the message to class is absolutely unnecessary (makes me wonder where you picked that up?): GameHUD sharedHUD addChild:sprite; And since you do this multiple times over, you should keep a temporary local copy of GameHUD, this again removes several unnecessary Objective-C messages: GameHUD* gameHUD = GameHUD sharedHUD; // from now on use gameHUD instead of GameHUD sharedHUD gameHUD addChild:sprite; This is particularly good advice for loops, because doing this: for (CCSprite *entity in GameHUD class sharedHUD buildable) will send two extra messages (class and sharedHUD) for every entity in the buildable array. Those extra calls can quickly add up, although they're certainly not enough for the drop in framerate you're experiencing.

You also unnecessarily keep all the HUD sprites in the "buildable" array. Why not use the already existing children array that cocos2d uses? Simply add each HUD sprite that is "buildable" with the same tag, for example 123.

GameHUD addChild:sprite z:0 tag:123; If you need to do something with all the "buildable" sprites you can then iterate over the children like this: CCNode* node; CCARRAY_FOREACH(gameHUD children, node) { if (node. Tag == 123) { CCSprite* buildable = (CCSprite*)node; // do stuff with buildable sprite ... } } Again, this avoids the unnecessary adding, retaining, removing and releasing of objects in the buildable array. And you can be sure that you don't accidentally remove sprites from the node hierarchy but not the buildable array, or vice versa.

I'd like to conclude with an assumption: in your code I saw a general tendency that you're doing many extra things unnecessarily.So I'm guessing this is the case throughout the project. You might want to go back and ask yourself (better: find out) what other things there are that you're having the device perform that are rather unnecessary.

Thanks, that's some great advice! I realize that there's a lot to improve in that code section (and probably the whole project).. I don't remember where I picked up the GameHUD class call.. I think I saw it as the only way to access a static method from within its class.. great idea with the tags, didn't think of them in this instance.. – dschihejns Nov 1 at 14:58.

If you are, then keep in mind that you can't profile an OpenGL app properly on the simulator .. The performance is too variable. Test it on a real device and check the frame rate again. If not, then you got a problem somewhere else.

All the processing done in this method are trivial, unless your images are 5000x5000 px or something..

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions